-
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.
feat(tests): added basic Categories tests for all the Commands/Queries
- Loading branch information
Showing
5 changed files
with
174 additions
and
2 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
41 changes: 41 additions & 0 deletions
41
tests/Application/UnitTests/Categories/DeleteAsync/DeleteCategoryCommandHandlerTests.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,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(); | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
tests/Application/UnitTests/Categories/PatchAsync/PatchCategoryCommandHandlerTests.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,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(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
tests/Application/UnitTests/Categories/PostAsync/PostCategoryCommandHandlerTests.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,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(); | ||
} | ||
} |