Skip to content

Commit

Permalink
[SOA-044][Testing] #ProblemOf[SOA-015] RE-structure the UnitTest proj…
Browse files Browse the repository at this point in the history
…ect, using Moq correctly (#82)
  • Loading branch information
LyQuocCuong authored Aug 22, 2023
1 parent fd3c138 commit 7c0516b
Show file tree
Hide file tree
Showing 28 changed files with 954 additions and 1,068 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public async Task<IActionResult> UpdateCompanyFullyAsync([FromRoute]Guid id,
}

bool result = await _services.Company.UpdateFullyAsync(id, updateDto);
if (result == false)
{
return BadRequest("Can not update");
}
return NoContent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public async Task<IActionResult> CreateEmployeeAsync([FromBody]EmployeeForCreati
return BadRequest("Object for creation is NULL.");
}
EmployeeDto employeeDto = await _services.Employee.CreateAsync(creationDto);

employeeDto.Id = Guid.Empty; // Edited
return CreatedAtRoute("GetEmployeeById", new { id = employeeDto.Id }, employeeDto);
}

Expand All @@ -55,6 +53,10 @@ public async Task<IActionResult> UpdateEmployeeFullyAsync([FromRoute]Guid id, [F
return NotFound("EmployeeId is non-existing.");
}
bool result = await _services.Employee.UpdateFullyAsync(id, updateDto);
if (result == false)
{
return BadRequest("Can NOT update");
}
return NoContent();
}

Expand Down
44 changes: 44 additions & 0 deletions MockCores/FakeDataShared/FakeDataForEntities/FakeDataForCompany.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
namespace FakeDataShared.FakeDataForEntities
{
public sealed class FakeDataForCompany
{
public Guid GetExistingCompanyId()
{
return new Guid("00000000-0000-0000-0000-000000000001");
}

public Guid GetNonExistingCompanyId()
{
return new Guid("00000000-0000-0000-0000-000000000009");
}

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

public CompanyForUpdateDto GetValidUpdateDto()
{
return new CompanyForUpdateDto()
{
Name = "Valid Object",
};
}

public CompanyForUpdateDto GetInvalidUpdateDto()
{
return new CompanyForUpdateDto()
{
Name = "Invalid Object",
};
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
namespace FakeDataShared.FakeDataForEntities
{
public sealed class FakeDataForEmployee
{
public Guid GetUnableDeletedEmployeeId()
{
return new Guid("00000000-0000-0000-0000-000000000100");
}

public Guid GetExistingEmployeeId()
{
return new Guid("00000000-0000-0000-0000-000000000102");
}

public Guid GetNewlyCreatedEmployeeId()
{
return new Guid("00000000-0000-0000-0000-000000000105");
}

public Guid GetNonExistingEmployeeId()
{
return new Guid("00000000-0000-0000-0000-000000000109");
}

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

public EmployeeForUpdateDto GetValidUpdateDto()
{
return new EmployeeForUpdateDto()
{
Code = "Valid Object",
};
}

public EmployeeForUpdateDto GetInvalidUpdateDto()
{
return new EmployeeForUpdateDto()
{
Code = "Invalid Object",
};
}

public EmployeeForCreationDto GetValidCreationDto()
{
return new EmployeeForCreationDto()
{
Code = "Valid Object",
};
}

public EmployeeDto GetNewlyCreatedEmployeeDto()
{
return new EmployeeDto()
{
Id = this.GetNewlyCreatedEmployeeId(),
Code = "NewlyCreated"
};
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
global using Shared.DTOs.Inputs.FromBody.CreationDtos;
global using Shared.DTOs.Inputs.FromBody.UpdateDtos;
global using Shared.DTOs.Outputs.EntityDtos;


39 changes: 39 additions & 0 deletions MockCores/MockContracts.Business/Entities/MockICompanyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace MockContracts.Business.Entities
{
internal sealed class MockICompanyService
{
public static Mock<ICompanyService> GetInstance()
{
var mockCompanyService = new Mock<ICompanyService>();

// Fake Data
FakeDataForCompany fakeDataForCompany = new FakeDataForCompany();
Guid nonExistingCompanyId = fakeDataForCompany.GetNonExistingCompanyId();
IEnumerable<CompanyDto> listOfCompanyDtos = fakeDataForCompany.GetListOfCompanyDtos();
CompanyForUpdateDto invalidUpdateDto = fakeDataForCompany.GetInvalidUpdateDto();

// SetUps
mockCompanyService
.Setup(s => s.GetByIdAsync(It.IsAny<Guid>()))
.ReturnsAsync((Guid companyId) => listOfCompanyDtos.FirstOrDefault(c => c.Id == companyId));

mockCompanyService
.Setup(s => s.GetAllAsync())
.ReturnsAsync(listOfCompanyDtos);

mockCompanyService
.Setup(s => s.IsValidIdAsync(It.IsAny<Guid>()))
.ReturnsAsync((Guid companyId) => companyId != nonExistingCompanyId);

mockCompanyService
.Setup(s => s.UpdateFullyAsync(It.IsAny<Guid>(), It.IsAny<CompanyForUpdateDto>()))
.ReturnsAsync((Guid companyId, CompanyForUpdateDto updateDto) =>
{
return (companyId != nonExistingCompanyId
&& updateDto.Name != invalidUpdateDto.Name);
});

return mockCompanyService;
}
}
}
59 changes: 59 additions & 0 deletions MockCores/MockContracts.Business/Entities/MockIEmployeeService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
namespace MockContracts.Business.Entities
{
internal sealed class MockIEmployeeService
{
public static Mock<IEmployeeService> GetInstance()
{
var mockEmployeeService = new Mock<IEmployeeService>();

// Fake Data
FakeDataForEmployee fakeDataForEmployee = new FakeDataForEmployee();
Guid nonExistingEmployeeId = fakeDataForEmployee.GetNonExistingEmployeeId();
Guid unableDeleteEmployeeId = fakeDataForEmployee.GetUnableDeletedEmployeeId();
IEnumerable<EmployeeDto> listOfEmployeeDtos = fakeDataForEmployee.GetListOfEmployeeDtos();
EmployeeForUpdateDto invalidUpdateDto = fakeDataForEmployee.GetInvalidUpdateDto();
EmployeeDto newlyCreatedEmployeeDto = fakeDataForEmployee.GetNewlyCreatedEmployeeDto();

// SetUps
mockEmployeeService
.Setup(s => s.GetByIdAsync(It.IsAny<Guid>()))
.ReturnsAsync((Guid employeeId) => listOfEmployeeDtos.FirstOrDefault(c => c.Id == employeeId));

mockEmployeeService
.Setup(s => s.GetAllAsync())
.ReturnsAsync(listOfEmployeeDtos);

mockEmployeeService
.Setup(s => s.IsValidIdAsync(It.IsAny<Guid>()))
.ReturnsAsync((Guid employeeId) => employeeId != nonExistingEmployeeId);

mockEmployeeService
.Setup(s => s.UpdateFullyAsync(It.IsAny<Guid>(), It.IsAny<EmployeeForUpdateDto>()))
.ReturnsAsync((Guid companyId, EmployeeForUpdateDto updateDto) =>
{
return (companyId != nonExistingEmployeeId
&& updateDto.Code != invalidUpdateDto.Code);
});

mockEmployeeService
.Setup(s => s.CreateAsync(It.IsAny<EmployeeForCreationDto>()))
.ReturnsAsync(newlyCreatedEmployeeDto);

mockEmployeeService
.Setup(s => s.DeleteSoftlyAsync(It.IsAny<Guid>()))
.ReturnsAsync((Guid employeeId) =>
{
return employeeId != unableDeleteEmployeeId;
});

mockEmployeeService
.Setup(s => s.DeleteHardAsync(It.IsAny<Guid>()))
.ReturnsAsync((Guid employeeId) =>
{
return employeeId != unableDeleteEmployeeId;
});

return mockEmployeeService;
}
}
}
6 changes: 6 additions & 0 deletions MockCores/MockContracts.Business/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global using Contracts.Business.Entities;
global using FakeDataShared.FakeDataForEntities;
global using Moq;
global using Shared.DTOs.Inputs.FromBody.CreationDtos;
global using Shared.DTOs.Inputs.FromBody.UpdateDtos;
global using Shared.DTOs.Outputs.EntityDtos;
27 changes: 27 additions & 0 deletions MockCores/MockContracts.Business/Managers/MockIServiceManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Contracts.Business.Managers;
using MockContracts.Business.Entities;

namespace MockContracts.Business.Managers
{
public class MockIServiceManager
{
public static Mock<IServiceManager> GetInstance()
{
// Entity Services
var mockCompanyService = MockICompanyService.GetInstance();
var mockEmployeeService = MockIEmployeeService.GetInstance();

// SetUps
var mockServiceManager = new Mock<IServiceManager>();
mockServiceManager
.Setup(s => s.Company)
.Returns(mockCompanyService.Object);

mockServiceManager
.Setup(s => s.Employee)
.Returns(mockEmployeeService.Object);

return mockServiceManager;
}
}
}
18 changes: 18 additions & 0 deletions MockCores/MockContracts.Business/MockContracts.Business.csproj
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>
<ProjectReference Include="..\..\Cores\Contracts.Business\Contracts.Business.csproj" />
<ProjectReference Include="..\FakeDataShared\FakeDataShared.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.18.0" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions MockCores/MockContracts/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global using Moq;

18 changes: 18 additions & 0 deletions MockCores/MockContracts/MockContracts.csproj
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="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Moq" Version="4.18.0" />
</ItemGroup>

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

</Project>
12 changes: 12 additions & 0 deletions MockCores/MockContracts/Utilities/Logger/MockILogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Logging;

namespace MockContracts.Utilities.Logger
{
public sealed class MockILogger<TController>
{
public static Mock<ILogger<TController>> GetInstance()
{
return new Mock<ILogger<TController>>();
}
}
}
12 changes: 12 additions & 0 deletions MockCores/MockContracts/Utilities/Mapper/MockIMapService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Contracts.Utilities.Mapper;

namespace MockContracts.Utilities.Mapper
{
public sealed class MockIMapService
{
public static Mock<IMapService> GetInstance()
{
return new Mock<IMapService>();
}
}
}
Loading

0 comments on commit 7c0516b

Please sign in to comment.