-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from barzin144/add-unit-tests
add unit test for user service
- Loading branch information
Showing
14 changed files
with
174 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DataAccess\DataAccess.csproj" /> | ||
<ProjectReference Include="..\Service\Service.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
global using Xunit; | ||
global using Moq; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Domain.Entities; | ||
using Domain.Repositories; | ||
using MongoDB.Driver; | ||
using Service; | ||
|
||
namespace DataAccess.test; | ||
|
||
public class UserRepositoryTest | ||
{ | ||
private UserRepository _userRepository; | ||
private SecurityService _securityService; | ||
private Mock<IMongoDbContext> _mongodbContext; | ||
private Mock<IBaseRepository<User>> _baseRepository; | ||
|
||
public UserRepositoryTest() | ||
{ | ||
_securityService = new SecurityService(); | ||
|
||
_mongodbContext = new Mock<IMongoDbContext>(); | ||
_baseRepository = new Mock<IBaseRepository<User>>(); | ||
// _mongoCollection = new Mock<IMongoCollection<User>>(); | ||
// var a = new Mock<IFindFluent<User, User>>(); | ||
// a.Setup(x => x.SingleOrDefaultAsync(default)).Returns(() => Task<User>.FromResult(new User | ||
// { | ||
// UserName = "a", | ||
// Password = "b" | ||
// })); | ||
|
||
// _mongodbContext.Setup(_ => _.GetCollection<User>("Users")).Returns(_mongoCollection.Object); | ||
|
||
// _mongoCollection.Setup(_ => _.Find(It.IsAny<FilterDefinition<User>>(), null)).Returns(() => a); | ||
|
||
_userRepository = new UserRepository(_mongodbContext.Object, _securityService); | ||
} | ||
[Fact] | ||
public void FindUserByUsernameAndPasswordAsync_ShouldFindUserByUsernameAndPassword() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading.Tasks; | ||
|
||
namespace Domain.Repositories | ||
{ | ||
public interface IBaseRepository<T> where T:class | ||
{ | ||
Task<bool> InsertOneAsync(T entity); | ||
Task<T> FindById(string Id); | ||
Task<T> FindByIdAsync(string Id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
global using Xunit; | ||
global using Moq; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" /> | ||
<PackageReference Include="Moq" Version="4.20.70" /> | ||
<PackageReference Include="xunit" Version="2.4.2" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Service\Service.csproj" /> | ||
<ProjectReference Include="..\Domain\Domain.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Domain.Repositories; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Service.test; | ||
|
||
public class UserServiceTest | ||
{ | ||
private Mock<IUserRepository> _userRepository; | ||
private readonly Mock<IHttpContextAccessor> _httpContextAccessor; | ||
private readonly SecurityService _securityService; | ||
private readonly UserService _userService; | ||
|
||
public UserServiceTest() | ||
{ | ||
_userRepository = new Mock<IUserRepository>(); | ||
_httpContextAccessor = new Mock<IHttpContextAccessor>(); | ||
_securityService = new SecurityService(); | ||
_userService = new UserService(_userRepository.Object, _httpContextAccessor.Object, _securityService); | ||
} | ||
[Fact] | ||
public async void FindUserByUsernameAndPasswordAsync_ShouldSendCorrectFilter() | ||
{ | ||
//Arrange | ||
string username = "abc"; | ||
string password = "abc"; | ||
string passwordHash = _securityService.GetSha256Hash(password); | ||
//Act | ||
var result = await _userService.FindUserByUsernameAndPasswordAsync(username, password); | ||
//Assert | ||
_userRepository.Verify(x => x.FindUserByUsernameAndPasswordAsync(s => s.UserName == username && s.Password == passwordHash), Times.Once); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters