-
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.
[SOA-014][Testing] Write the UnitTest for "eShopOnlineBusiness" - BAS…
…IC (just CompanyService, EmployeeService) (#85)
- Loading branch information
1 parent
af447a7
commit 10aff7a
Showing
24 changed files
with
712 additions
and
22 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
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
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,5 +1,5 @@ | ||
global using Shared.DTOs.Inputs.FromBody.CreationDtos; | ||
global using Domains.Entities; | ||
global using Shared.DTOs.Inputs.FromBody.CreationDtos; | ||
global using Shared.DTOs.Inputs.FromBody.UpdateDtos; | ||
global using Shared.DTOs.Outputs.EntityDtos; | ||
|
||
|
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
30 changes: 30 additions & 0 deletions
30
MockCores/MockContracts.Repositories/Entities/MockICompanyRepository.cs
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,30 @@ | ||
namespace MockContracts.Repositories.Entities | ||
{ | ||
internal sealed class MockICompanyRepository | ||
{ | ||
public static Mock<ICompanyRepository> GetInstance() | ||
{ | ||
// Fake Data | ||
var fakeDataForCompany = new FakeDataForCompany(); | ||
Guid nonExistingCompanyId = fakeDataForCompany.GetNonExistingCompanyId(); | ||
var listOfCompanies = fakeDataForCompany.GetListOfCompanies(); | ||
|
||
// SetUps | ||
var mockCompanyRepo = new Mock<ICompanyRepository>(MockBehavior.Strict); | ||
|
||
mockCompanyRepo | ||
.Setup(s => s.GetAllAsync(It.IsAny<bool>())) | ||
.ReturnsAsync(listOfCompanies); | ||
|
||
mockCompanyRepo | ||
.Setup(s => s.GetByIdAsync(It.IsAny<bool>(), It.IsAny<Guid>())) | ||
.ReturnsAsync((bool isTrackChanges, Guid companyId) => listOfCompanies.FirstOrDefault(c => c.Id == companyId)); | ||
|
||
mockCompanyRepo | ||
.Setup(s => s.IsValidIdAsync(It.IsAny<Guid>())) | ||
.ReturnsAsync((Guid companyId) => companyId != nonExistingCompanyId); | ||
|
||
return mockCompanyRepo; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
MockCores/MockContracts.Repositories/Entities/MockIEmployeeRepository.cs
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,44 @@ | ||
namespace MockContracts.Repositories.Entities | ||
{ | ||
internal sealed class MockIEmployeeRepository | ||
{ | ||
public static Mock<IEmployeeRepository> GetInstance() | ||
{ | ||
// Fake Data | ||
var fakeDataForEmployee = new FakeDataForEmployee(); | ||
Guid nonExistingEmployeeId = fakeDataForEmployee.GetNonExistingEmployeeId(); | ||
var listOfEmployees = fakeDataForEmployee.GetListOfEmployees(); | ||
|
||
// SetUps | ||
var mockEmployeeRepo = new Mock<IEmployeeRepository>(MockBehavior.Strict); | ||
|
||
mockEmployeeRepo | ||
.Setup(s => s.GetAllAsync(It.IsAny<bool>())) | ||
.ReturnsAsync(listOfEmployees); | ||
|
||
mockEmployeeRepo | ||
.Setup(s => s.GetByIdAsync(It.IsAny<bool>(), It.IsAny<Guid>())) | ||
|
||
.ReturnsAsync((bool isTrackChanges, Guid employeeId) | ||
=> listOfEmployees.FirstOrDefault(e => e.Id == employeeId)); | ||
|
||
mockEmployeeRepo | ||
.Setup(s => s.IsValidIdAsync(It.IsAny<Guid>())) | ||
.ReturnsAsync(((Guid employeeId) => employeeId != nonExistingEmployeeId)); | ||
|
||
mockEmployeeRepo | ||
.Setup(s => s.Create(It.IsAny<Employee>())) | ||
.Callback(() => { return; }); // void method | ||
|
||
mockEmployeeRepo | ||
.Setup(s => s.DeleteSoftly(It.IsAny<Employee>())) | ||
.Callback(() => { return; }); // void method | ||
|
||
mockEmployeeRepo | ||
.Setup(s => s.DeleteHard(It.IsAny<Employee>())) | ||
.Callback(() => { return; }); // void method | ||
|
||
return mockEmployeeRepo; | ||
} | ||
} | ||
} |
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,4 @@ | ||
global using Contracts.Repositories.Entities; | ||
global using Domains.Entities; | ||
global using FakeDataShared.FakeDataForEntities; | ||
global using Moq; |
32 changes: 32 additions & 0 deletions
32
MockCores/MockContracts.Repositories/Managers/MockIRepositoryManager.cs
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 Contracts.Repositories.Managers; | ||
using MockContracts.Repositories.Entities; | ||
|
||
namespace MockContracts.Repositories.Managers | ||
{ | ||
public sealed class MockIRepositoryManager | ||
{ | ||
public static Mock<IRepositoryManager> GetInstance() | ||
{ | ||
// Entity Repos | ||
var mockCompanyRepo = MockICompanyRepository.GetInstance(); | ||
var mockEmployeeRepo = MockIEmployeeRepository.GetInstance(); | ||
|
||
// SetUps | ||
var mockRepoManager = new Mock<IRepositoryManager>(MockBehavior.Strict); | ||
|
||
mockRepoManager | ||
.Setup(s => s.SaveChangesAsync()) | ||
.Returns(Task.CompletedTask); // Task of (void) method | ||
|
||
mockRepoManager | ||
.Setup(s => s.Company) | ||
.Returns(mockCompanyRepo.Object); | ||
|
||
mockRepoManager | ||
.Setup(s => s.Employee) | ||
.Returns(mockEmployeeRepo.Object); | ||
|
||
return mockRepoManager; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
MockCores/MockContracts.Repositories/MockContracts.Repositories.csproj
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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Moq" Version="4.18.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Cores\Contracts.Repositories\Contracts.Repositories.csproj" /> | ||
<ProjectReference Include="..\FakeDataShared\FakeDataShared.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
4 changes: 2 additions & 2 deletions
4
...Contracts/Utilities/Logger/MockILogger.cs → ...racts/Utilities/LogService/MockILogger.cs
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,12 +1,12 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace MockContracts.Utilities.Logger | ||
namespace MockContracts.Utilities.LogService | ||
{ | ||
public sealed class MockILogger<TController> | ||
{ | ||
public static Mock<ILogger<TController>> GetInstance() | ||
{ | ||
return new Mock<ILogger<TController>>(); | ||
return new Mock<ILogger<TController>>(MockBehavior.Loose); // just for Initializing Constructor | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
MockCores/MockContracts/Utilities/MapService/FakeAutoMapperService.cs
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,22 @@ | ||
using AutoMapper; | ||
using eShopOnlineUtilities.AutoMapper; | ||
using MockContracts.Utilities.LogService; | ||
|
||
namespace MockContracts.Utilities.MapService | ||
{ | ||
public sealed class FakeAutoMapperService | ||
{ | ||
private static IMapper GetMapperInstance() | ||
{ | ||
var mappingProfile = new eShopOnlineUtilities.AutoMapper.Profiles.MappingProfiles(); | ||
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(mappingProfile)); | ||
return new Mapper(configuration); | ||
} | ||
|
||
public static AutoMapperService GetInstance() | ||
{ | ||
var mockILogger = MockILogger<AutoMapperService>.GetInstance(); | ||
return new AutoMapperService(mockILogger.Object, GetMapperInstance()); | ||
} | ||
} | ||
} |
12 changes: 0 additions & 12 deletions
12
MockCores/MockContracts/Utilities/Mapper/MockIMapService.cs
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
UnitTests/UnitTest.eShopOnlineApiRestful/Abstracts/AbstractControllerTest.cs
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
27 changes: 27 additions & 0 deletions
27
UnitTests/UnitTest.eShopOnlineBusiness/Abstracts/AbstractServiceTest.cs
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,27 @@ | ||
using eShopOnlineBusiness.Parameters; | ||
using Microsoft.Extensions.Logging; | ||
using MockContracts.Repositories.Managers; | ||
using MockContracts.Utilities.LogService; | ||
using MockContracts.Utilities.MapService; | ||
|
||
namespace UnitTest.eShopOnlineBusiness.Abstracts | ||
{ | ||
public abstract class AbstractServiceTest<TService> | ||
{ | ||
protected abstract TService InitService(); | ||
|
||
protected ILogger<TService> GetILogger() | ||
{ | ||
return MockILogger<TService>.GetInstance().Object; | ||
} | ||
|
||
protected ServiceParams GetServiceParams() | ||
{ | ||
return new ServiceParams( | ||
MockIRepositoryManager.GetInstance().Object, | ||
FakeAutoMapperService.GetInstance() | ||
); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.