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(); + } +}