From b41b33232688d80cb4fbfb5df98463f12785695d Mon Sep 17 00:00:00 2001 From: thisisnabi Date: Mon, 3 Jun 2024 13:32:29 +0330 Subject: [PATCH] Add MakeDraftCommandHandlerTests tests --- .../Articles/MakeDraftCommandHandlerTests.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/Blogger.IntegrationTests/Articles/MakeDraftCommandHandlerTests.cs diff --git a/tests/Blogger.IntegrationTests/Articles/MakeDraftCommandHandlerTests.cs b/tests/Blogger.IntegrationTests/Articles/MakeDraftCommandHandlerTests.cs new file mode 100644 index 0000000..975048e --- /dev/null +++ b/tests/Blogger.IntegrationTests/Articles/MakeDraftCommandHandlerTests.cs @@ -0,0 +1,54 @@ +using Blogger.Application.Articles.GetTags; +using Blogger.Application.Articles.MakeDraft; +using Blogger.Domain.ArticleAggregate; +using Blogger.Infrastructure.Persistence.Repositories; +using Blogger.IntegrationTests.Fixtures; + +using FluentAssertions; + +namespace Blogger.IntegrationTests.Articles; +public class MakeDraftCommandHandlerTests : IClassFixture +{ + private readonly BloggerDbContextFixture _fixture; + + public MakeDraftCommandHandlerTests(BloggerDbContextFixture fixture) + { + _fixture = fixture; + } + + [Fact] + public async Task Handle_ShouldCreateDraft_WhenDraftDoesNotExist() + { + // Arrange + var request = new MakeDraftCommand("Existing Draft", "Draft body", "Draft summary", []); + var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); + var sut = new MakeDraftCommandHandler(articleRepository); + + // Act + var response = await sut.Handle(request, CancellationToken.None); + + // Assert + response.Should().NotBeNull(); + response.DraftId.Should().Be(ArticleId.CreateUniqueId(request.Title)); + } + + [Fact] + public async Task Handle_ShouldThrowDraftAlreadyExistsException_WhenDraftAlreadyExists() + { + // Arrange + var request = new MakeDraftCommand("Existing Draft", "Draft body", "Draft summary", []); + var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); + var sut = new MakeDraftCommandHandler(articleRepository); + + var oldDraft = Article.CreateDraft("Existing Draft", "Draft body", "Draft summary"); + + articleRepository.Add(oldDraft); + await articleRepository.SaveChangesAsync(CancellationToken.None); + + // Act + var draft = async () => await sut.Handle(request, CancellationToken.None); + + // Assert + await draft.Should().ThrowAsync(); + } +}