Skip to content

Commit

Permalink
feat(tests): added basic Categories tests for all the Commands/Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mezdelex committed Aug 4, 2024
1 parent 8871b2a commit 989fd46
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

namespace Application.Expenses.GetAllAsync;

public record GetAllExpensesQuery(int Page, int PageSize) : IRequest<PagedList<ExpenseDTO>>;
public record GetAllExpensesQuery(int Page, int PageSize) : IRequest<PagedList<ExpenseDTO>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Application.Categories.DeleteAsync;
using Domain.Categories;
using Domain.Persistence;
using Moq;

namespace Application.UnitTests.Categories.PostAsync;

public sealed class DeleteCategoryCommandHandlerTests
{
private readonly CancellationToken _cancellationToken;
private readonly Mock<ICategoriesRepository> _repository;
private readonly Mock<IUnitOfWork> _uow;
private readonly DeleteCategoryCommandHandler _handler;

public DeleteCategoryCommandHandlerTests()
{
_cancellationToken = new();
_repository = new();
_uow = new();

_handler = new DeleteCategoryCommandHandler(_repository.Object, _uow.Object);
}

[Fact]
public async Task DeleteCategoryCommandHandler_ShouldDeleteCategory()
{
// Arrange
var deleteCategoryCommand = new DeleteCategoryCommand(Guid.NewGuid());
_repository
.Setup(mock => mock.DeleteAsync(It.IsAny<Guid>(), _cancellationToken))
.Verifiable();
_uow.Setup(mock => mock.SaveChangesAsync(_cancellationToken)).Verifiable();

// Act
await _handler.Handle(deleteCategoryCommand, _cancellationToken);

// Assert
_repository.Verify();
_uow.Verify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public sealed class GetAllCategoriesQueryHandlerTests
private readonly CancellationToken _cancellationToken;
private readonly Mock<DbSet<Category>> _dbSet;
private readonly Mock<IApplicationDbContext> _context;
private readonly GetAllCategoriesQueryHandler _handler;
private readonly Mock<IRedisCache> _redisCache;
private readonly GetAllCategoriesQueryHandler _handler;

public GetAllCategoriesQueryHandlerTests()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Application.Abstractions;
using Application.Categories.PatchAsync;
using Domain.Categories;
using Domain.Persistence;
using FluentValidation;
using FluentValidation.Results;
using Moq;

namespace Application.UnitTests.Categories.PostAsync;

public sealed class PatchCategoryCommandHandlerTests
{
private readonly CancellationToken _cancellationToken;
private readonly Mock<IValidator<PatchCategoryCommand>> _validator;
private readonly Mock<ICategoriesRepository> _repository;
private readonly Mock<IUnitOfWork> _uow;
private readonly Mock<IEventBus> _eventBus;
private readonly PatchCategoryCommandHandler _handler;

public PatchCategoryCommandHandlerTests()
{
_cancellationToken = new();
_validator = new();
_repository = new();
_uow = new();
_eventBus = new();

_handler = new PatchCategoryCommandHandler(
_validator.Object,
_repository.Object,
_uow.Object,
_eventBus.Object
);
}

[Fact]
public async Task PatchCategoryCommandHandler_ShouldPatchCategoryAndPublishEventAsync()
{
// Arrange
var patchCategoryCommand = new PatchCategoryCommand(
Guid.NewGuid(),
"Category 1 name",
"Category 1 description"
);
_validator
.Setup(mock => mock.ValidateAsync(patchCategoryCommand, _cancellationToken))
.ReturnsAsync(new ValidationResult())
.Verifiable();
_repository
.Setup(mock => mock.PatchAsync(It.IsAny<Category>(), _cancellationToken))
.Verifiable();
_uow.Setup(mock => mock.SaveChangesAsync(_cancellationToken)).Verifiable();
_eventBus
.Setup(mock => mock.PublishAsync(It.IsAny<PatchedCategoryEvent>(), _cancellationToken))
.Verifiable();

// Act
await _handler.Handle(patchCategoryCommand, _cancellationToken);

// Assert
_validator.Verify();
_repository.Verify();
_uow.Verify();
_eventBus.Verify();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Application.Abstractions;
using Application.Categories.PostAsync;
using Domain.Categories;
using Domain.Persistence;
using FluentValidation;
using FluentValidation.Results;
using Moq;

namespace Application.UnitTests.Categories.PostAsync;

public sealed class PostCategoryCommandHandlerTests
{
private readonly CancellationToken _cancellationToken;
private readonly Mock<IValidator<PostCategoryCommand>> _validator;
private readonly Mock<ICategoriesRepository> _repository;
private readonly Mock<IUnitOfWork> _uow;
private readonly Mock<IEventBus> _eventBus;
private readonly PostCategoryCommandHandler _handler;

public PostCategoryCommandHandlerTests()
{
_cancellationToken = new();
_validator = new();
_repository = new();
_uow = new();
_eventBus = new();

_handler = new PostCategoryCommandHandler(
_validator.Object,
_repository.Object,
_uow.Object,
_eventBus.Object
);
}

[Fact]
public async Task PostCategoryCommandHandler_ShouldPostCategoryAndPublishEventAsync()
{
// Arrange
var postCategoryCommand = new PostCategoryCommand(
"Category 1 name",
"Category 1 description"
);
_validator
.Setup(mock => mock.ValidateAsync(postCategoryCommand, _cancellationToken))
.ReturnsAsync(new ValidationResult())
.Verifiable();
_repository
.Setup(mock => mock.PostAsync(It.IsAny<Category>(), _cancellationToken))
.Verifiable();
_uow.Setup(mock => mock.SaveChangesAsync(_cancellationToken)).Verifiable();
_eventBus
.Setup(mock => mock.PublishAsync(It.IsAny<PostedCategoryEvent>(), _cancellationToken))
.Verifiable();

// Act
await _handler.Handle(postCategoryCommand, _cancellationToken);

// Assert
_validator.Verify();
_repository.Verify();
_uow.Verify();
_eventBus.Verify();
}
}

0 comments on commit 989fd46

Please sign in to comment.