Skip to content

Commit

Permalink
[SOA-014][Testing] Write the UnitTest for "eShopOnlineBusiness" - BAS…
Browse files Browse the repository at this point in the history
…IC (just CompanyService, EmployeeService) (#85)
  • Loading branch information
LyQuocCuong authored Aug 23, 2023
1 parent af447a7 commit 10aff7a
Show file tree
Hide file tree
Showing 24 changed files with 712 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public sealed class CompanyService : AbstractService<CompanyService>, ICompanyService
{
internal CompanyService(ILogger<CompanyService> logger,
public CompanyService(ILogger<CompanyService> logger,
ServiceParams serviceParams)
: base(logger, serviceParams)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public sealed class EmployeeService : AbstractService<EmployeeService>, IEmployeeService
{
internal EmployeeService(ILogger<EmployeeService> logger,
public EmployeeService(ILogger<EmployeeService> logger,
ServiceParams serviceParams)
: base(logger, serviceParams)
{
Expand Down
12 changes: 12 additions & 0 deletions MockCores/FakeDataShared/FakeDataForEntities/FakeDataForCompany.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public IEnumerable<CompanyDto> GetListOfCompanyDtos()
};
}

public IEnumerable<Company> GetListOfCompanies()
{
return new List<Company>()
{
new Company()
{
Id = this.GetExistingCompanyId(),
Name = "Company 01"
}
};
}

public CompanyForUpdateDto GetValidUpdateDto()
{
return new CompanyForUpdateDto()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public IEnumerable<EmployeeDto> GetListOfEmployeeDtos()
};
}

public IEnumerable<Employee> GetListOfEmployees()
{
return new List<Employee>()
{
new Employee()
{
Id = this.GetExistingEmployeeId(),
Code = "E001"
}
};
}

public EmployeeForUpdateDto GetValidUpdateDto()
{
return new EmployeeForUpdateDto()
Expand Down
1 change: 1 addition & 0 deletions MockCores/FakeDataShared/FakeDataShared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Cores\Domains\Domains.csproj" />
<ProjectReference Include="..\..\Cores\Shared\Shared.csproj" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions MockCores/FakeDataShared/GlobalUsings.cs
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;


Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace MockContracts.Business.Managers
{
public class MockIServiceManager
public sealed class MockIServiceManager
{
public static Mock<IServiceManager> GetInstance()
{
Expand Down
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;
}
}
}
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;
}
}
}
4 changes: 4 additions & 0 deletions MockCores/MockContracts.Repositories/GlobalUsings.cs
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;
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;
}
}
}
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>
1 change: 1 addition & 0 deletions MockCores/MockContracts/MockContracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<ProjectReference Include="..\..\Cores\Contracts\Contracts.csproj" />
<ProjectReference Include="..\..\Implementations\eShopOnlineUtilities\eShopOnlineUtilities.csproj" />
</ItemGroup>

</Project>
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
}
}
}
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 MockCores/MockContracts/Utilities/Mapper/MockIMapService.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using eShopOnlineApiRestful.Parameters;
using Microsoft.Extensions.Logging;
using MockContracts.Business.Managers;
using MockContracts.Utilities.Logger;
using MockContracts.Utilities.LogService;

namespace UnitTest.eShopOnlineApiRestful.Abstracts
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public async Task GetAllCompaniesAsync_Inputs_Parameterless_Returns_OkObjectResu
public async Task GetAllCompaniesAsync_Inputs_Parameterless_Returns_OkObjectResult_Include_Collection_Has_UniqueCompanyIds()
{
// Arrange
var fakeDataForCompany = new FakeDataForCompany();
var expectedCollectionData = fakeDataForCompany.GetListOfCompanyDtos();
var companyController = InitController();

// Act
Expand Down
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()
);
}

}
}
Loading

0 comments on commit 10aff7a

Please sign in to comment.