Skip to content

Commit

Permalink
chore(test): added sln and specifications mock; pending IAsyncQueryPr…
Browse files Browse the repository at this point in the history
…ovider mock implementation for GetAllEntitiesAsync tests
  • Loading branch information
mezdelex committed Nov 12, 2024
1 parent 08c0fc1 commit 27876dd
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 63 deletions.
64 changes: 64 additions & 0 deletions CleanTemplate.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{70E85DD6-6976-4972-971F-20AB6F716167}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "src\Application\Application.csproj", "{939F0D55-76BA-4CCA-9D94-8F5A0487F091}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "src\Domain\Domain.csproj", "{6C042D02-CF7A-47E9-9A8E-03BDE02325B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "src\Infrastructure\Infrastructure.csproj", "{73911E11-F925-4058-B393-77B1F4D738ED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Presentation", "src\Presentation\Presentation.csproj", "{C9A2CF98-BC09-416D-87EE-B54CFAFD1A7F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApi", "src\WebApi\WebApi.csproj", "{10695739-E6BB-4DEA-9E5C-6E44745421AF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{6BCB7DA5-7FAE-4813-89A0-3FA80678D1F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "tests\UnitTests\UnitTests.csproj", "{ADDCFA13-B3C9-48A8-8DB2-5CE6A8EAA9FE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{939F0D55-76BA-4CCA-9D94-8F5A0487F091}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{939F0D55-76BA-4CCA-9D94-8F5A0487F091}.Debug|Any CPU.Build.0 = Debug|Any CPU
{939F0D55-76BA-4CCA-9D94-8F5A0487F091}.Release|Any CPU.ActiveCfg = Release|Any CPU
{939F0D55-76BA-4CCA-9D94-8F5A0487F091}.Release|Any CPU.Build.0 = Release|Any CPU
{6C042D02-CF7A-47E9-9A8E-03BDE02325B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C042D02-CF7A-47E9-9A8E-03BDE02325B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C042D02-CF7A-47E9-9A8E-03BDE02325B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C042D02-CF7A-47E9-9A8E-03BDE02325B3}.Release|Any CPU.Build.0 = Release|Any CPU
{73911E11-F925-4058-B393-77B1F4D738ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{73911E11-F925-4058-B393-77B1F4D738ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{73911E11-F925-4058-B393-77B1F4D738ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{73911E11-F925-4058-B393-77B1F4D738ED}.Release|Any CPU.Build.0 = Release|Any CPU
{C9A2CF98-BC09-416D-87EE-B54CFAFD1A7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C9A2CF98-BC09-416D-87EE-B54CFAFD1A7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C9A2CF98-BC09-416D-87EE-B54CFAFD1A7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C9A2CF98-BC09-416D-87EE-B54CFAFD1A7F}.Release|Any CPU.Build.0 = Release|Any CPU
{10695739-E6BB-4DEA-9E5C-6E44745421AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10695739-E6BB-4DEA-9E5C-6E44745421AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10695739-E6BB-4DEA-9E5C-6E44745421AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10695739-E6BB-4DEA-9E5C-6E44745421AF}.Release|Any CPU.Build.0 = Release|Any CPU
{ADDCFA13-B3C9-48A8-8DB2-5CE6A8EAA9FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ADDCFA13-B3C9-48A8-8DB2-5CE6A8EAA9FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ADDCFA13-B3C9-48A8-8DB2-5CE6A8EAA9FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ADDCFA13-B3C9-48A8-8DB2-5CE6A8EAA9FE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{939F0D55-76BA-4CCA-9D94-8F5A0487F091} = {70E85DD6-6976-4972-971F-20AB6F716167}
{6C042D02-CF7A-47E9-9A8E-03BDE02325B3} = {70E85DD6-6976-4972-971F-20AB6F716167}
{73911E11-F925-4058-B393-77B1F4D738ED} = {70E85DD6-6976-4972-971F-20AB6F716167}
{C9A2CF98-BC09-416D-87EE-B54CFAFD1A7F} = {70E85DD6-6976-4972-971F-20AB6F716167}
{10695739-E6BB-4DEA-9E5C-6E44745421AF} = {70E85DD6-6976-4972-971F-20AB6F716167}
{ADDCFA13-B3C9-48A8-8DB2-5CE6A8EAA9FE} = {6BCB7DA5-7FAE-4813-89A0-3FA80678D1F1}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,61 @@ namespace UnitTests.Features.Queries;
public sealed class GetAllCategoriesQueryHandlerTests
{
private readonly CancellationToken _cancellationToken;
private readonly Mock<DbSet<Category>> _dbSet;
private readonly Mock<IApplicationDbContext> _context;
private readonly Mock<ICategoriesRepository> _repository;
private readonly Mock<IRedisCache> _redisCache;
private readonly GetAllCategoriesQueryHandler _handler;

public GetAllCategoriesQueryHandlerTests()
{
_cancellationToken = new();
_dbSet = new();
_context = new();
_repository = new();
_redisCache = new();

_handler = new GetAllCategoriesQueryHandler(_context.Object, _redisCache.Object);
_handler = new GetAllCategoriesQueryHandler(_repository.Object, _redisCache.Object);
}

[Fact]
[Fact(Skip = "Pending IAsyncQueryProvider mock implementation")]
public async Task GetAllCategoriesQueryHandler_ShouldReturnPagedListOfRequestedCategoriesAsListOfCategoryDTOAndMetadata()
{
// Arrange
var name = "Test";
var containedWord = "es";
var page = 1;
var pageSize = 2;
var getAllCategoriesQuery = new GetAllCategoriesQuery(page, pageSize);
var getAllCategoriesQuery = new GetAllCategoriesQuery
{
Page = page,
PageSize = pageSize,
Name = name,
ContainedWord = containedWord,
};
var redisKey = $"{nameof(GetAllCategoriesQuery)}#{page}#{pageSize}";
var categories = new List<Category>
{
new(Guid.NewGuid(), "Name 1", "Description 1"),
new(Guid.NewGuid(), "Name 2", "Description 2"),
new()
{
Id = Guid.NewGuid(),
Name = "Name 1",
Description = "Description 1",
},
new()
{
Id = Guid.NewGuid(),
Name = "Name 2",
Description = "Description 2",
},
};
_dbSet
.As<IAsyncEnumerable<Category>>()
.Setup(mock => mock.GetAsyncEnumerator(It.IsAny<CancellationToken>()))
.Returns(new TestAsyncEnumerator<Category>(categories.AsQueryable().GetEnumerator()));
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.Provider)
.Returns(new TestAsyncQueryProvider<Category>(categories.AsQueryable().Provider));
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.Expression)
.Returns(categories.AsQueryable().Expression);
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.ElementType)
.Returns(categories.AsQueryable().ElementType);
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.GetEnumerator())
.Returns(categories.AsQueryable().GetEnumerator());
_context.Setup(mock => mock.Categories).Returns(_dbSet.Object).Verifiable();
var pagedCategories = new PagedList<Category>(
categories,
page,
pageSize,
categories.Count,
false,
false
);
_repository
.Setup(mock => mock.ApplySpecification(It.IsAny<CategoriesSpecification>()))
.Returns(categories.AsQueryable);
_redisCache
.Setup(mock => mock.GetCachedData<PagedList<CategoryDTO>>(redisKey))
.ReturnsAsync((PagedList<CategoryDTO>)null!);
Expand All @@ -63,7 +69,8 @@ public async Task GetAllCategoriesQueryHandler_ShouldReturnPagedListOfRequestedC
It.IsAny<DateTimeOffset>()
)
)
.Returns(Task.CompletedTask);
.Returns(Task.CompletedTask)
.Verifiable();

// Act
var result = await _handler.Handle(getAllCategoriesQuery, _cancellationToken);
Expand All @@ -80,7 +87,10 @@ public async Task GetAllCategoriesQueryHandler_ShouldReturnPagedListOfRequestedC
result.PageSize.Should().Be(pageSize);
result.HasPreviousPage.Should().Be(false);
result.HasNextPage.Should().Be(false);
_context.Verify();
_repository.Verify(
mock => mock.ApplySpecification(It.IsAny<CategoriesSpecification>()),
Times.Once
);
_redisCache.Verify(
mock => mock.GetCachedData<PagedList<CategoryDTO>>(redisKey),
Times.Once
Expand Down
53 changes: 22 additions & 31 deletions tests/UnitTests/Features/Queries/GetCategoryQueryHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ namespace Application.UnitTests.Categories.GetAsync;
public sealed class GetCategoryQueryHandlerTests
{
private readonly CancellationToken _cancellationToken;
private readonly Mock<DbSet<Category>> _dbSet;
private readonly Mock<IApplicationDbContext> _context;
private readonly Mock<ICategoriesRepository> _repository;
private readonly GetCategoryQueryHandler _handler;

public GetCategoryQueryHandlerTests()
{
_cancellationToken = new();
_dbSet = new();
_context = new();
_repository = new();

_handler = new GetCategoryQueryHandler(_context.Object);
_handler = new GetCategoryQueryHandler(_repository.Object);
}

[Fact]
Expand All @@ -22,36 +20,29 @@ public async Task Handle_ValidIdGetCategoryQuery_ShouldReturnRequestedCategoryAs
// Arrange
var guid = Guid.NewGuid();
var getCategoryQuery = new GetCategoryQuery(guid);
var categories = new List<Category> { new(guid, "Name 1", "Description 1") };
_dbSet
.As<IAsyncEnumerable<Category>>()
.Setup(mock => mock.GetAsyncEnumerator(It.IsAny<CancellationToken>()))
.Returns(new TestAsyncEnumerator<Category>(categories.AsQueryable().GetEnumerator()));
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.Provider)
.Returns(new TestAsyncQueryProvider<Category>(categories.AsQueryable().Provider));
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.Expression)
.Returns(categories.AsQueryable().Expression);
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.ElementType)
.Returns(categories.AsQueryable().ElementType);
_dbSet
.As<IQueryable<Category>>()
.Setup(mock => mock.GetEnumerator())
.Returns(categories.AsQueryable().GetEnumerator());
_context.Setup(mock => mock.Categories).Returns(_dbSet.Object).Verifiable();
var category = new Category
{
Id = guid,
Name = "Name 1",
Description = "Description 1",
};
_repository
.Setup(mock =>
mock.GetBySpecAsync(It.IsAny<CategoriesSpecification>(), _cancellationToken)
)
.ReturnsAsync(category)
.Verifiable();

// Act
var result = await _handler.Handle(getCategoryQuery, _cancellationToken);

// Assert
result.Id.Should().Be(categories[0].Id);
result.Name.Should().Be(categories[0].Name);
result.Description.Should().Be(categories[0].Description);
_context.Verify();
result.Id.Should().Be(category.Id);
result.Name.Should().Be(category.Name);
result.Description.Should().Be(category.Description);
_repository.Verify(
mock => mock.GetBySpecAsync(It.IsAny<CategoriesSpecification>(), _cancellationToken),
Times.Once
);
}
}
1 change: 1 addition & 0 deletions tests/UnitTests/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
global using Domain.Entities;
global using static Domain.Extensions.Collections.Collections;
global using Domain.Persistence;
global using Domain.Specifications;
global using FluentAssertions;
global using FluentValidation;
global using FluentValidation.Results;
Expand Down

0 comments on commit 27876dd

Please sign in to comment.