-
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-044][Testing] #ProblemOf[SOA-015] RE-structure the UnitTest proj…
…ect, using Moq correctly (#82)
- Loading branch information
1 parent
fd3c138
commit 7c0516b
Showing
28 changed files
with
954 additions
and
1,068 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
44 changes: 44 additions & 0 deletions
44
MockCores/FakeDataShared/FakeDataForEntities/FakeDataForCompany.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 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", | ||
}; | ||
} | ||
|
||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
MockCores/FakeDataShared/FakeDataForEntities/FakeDataForEmployee.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,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" | ||
}; | ||
} | ||
|
||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
...ShopOnlineTestingUtilities/GlobalUsing.cs → MockCores/FakeDataShared/GlobalUsings.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,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
39
MockCores/MockContracts.Business/Entities/MockICompanyService.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,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
59
MockCores/MockContracts.Business/Entities/MockIEmployeeService.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,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; | ||
} | ||
} | ||
} |
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,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
27
MockCores/MockContracts.Business/Managers/MockIServiceManager.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 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
18
MockCores/MockContracts.Business/MockContracts.Business.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> | ||
<ProjectReference Include="..\..\Cores\Contracts.Business\Contracts.Business.csproj" /> | ||
<ProjectReference Include="..\FakeDataShared\FakeDataShared.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Moq" Version="4.18.0" /> | ||
</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 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,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> |
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,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
12
MockCores/MockContracts/Utilities/Mapper/MockIMapService.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,12 @@ | ||
using Contracts.Utilities.Mapper; | ||
|
||
namespace MockContracts.Utilities.Mapper | ||
{ | ||
public sealed class MockIMapService | ||
{ | ||
public static Mock<IMapService> GetInstance() | ||
{ | ||
return new Mock<IMapService>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.