Skip to content

Commit

Permalink
Add MakeDraftCommandHandlerTests tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnabi committed Jun 3, 2024
1 parent d83a3de commit b41b332
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<BloggerDbContextFixture>
{
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<DraftAlreadyExistsException>();
}
}

0 comments on commit b41b332

Please sign in to comment.