Skip to content

Commit

Permalink
Add subscriber integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisnabi committed Jun 17, 2024
1 parent 51d89d0 commit 92041c5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

using FluentAssertions;

using static Blogger.Application.ApplicationSettings;

namespace Blogger.IntegrationTests.Comments;

public class GetRepliesHandlerTests : IClassFixture<BloggerDbContextFixture>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Blogger.Application.Subscribers;
using Blogger.Application.Subscribers.Subscribe;
using Blogger.Domain.SubscriberAggregate;
using Blogger.Infrastructure.Persistence.Repositories;
using Blogger.IntegrationTests.Fixtures;

using FluentAssertions;

namespace Blogger.IntegrationTests.Subscribers;
public class SubscribeCommandHandlerTests : IClassFixture<BloggerDbContextFixture>
{
private readonly BloggerDbContextFixture _fixture;

public SubscribeCommandHandlerTests(BloggerDbContextFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task Handle_DuplicateSubscriber_ShouldThrowDuplicateSubscriptionException()
{
// Arrange
var dbContext = _fixture.BuildDbContext(Guid.NewGuid().ToString());
var subscribeRepository = new SubscriberRepository(dbContext);

var subscriberId = SubscriberId.CreateUniqueId("[email protected]");
var subscriber = Subscriber.Create(subscriberId);
await subscribeRepository.CreateAsync(subscriber, CancellationToken.None);
await subscribeRepository.SavaChangesAsync(CancellationToken.None);

var subscriberService = new SubscriberService(subscribeRepository);

var sut = new SubscribeCommandHandler(subscribeRepository, subscriberService);

var command = new SubscribeCommand(subscriberId);

// Act
Func<Task> act = async () => await sut.Handle(command, CancellationToken.None);

// Assert
await act.Should().ThrowAsync<DuplicateSubscribtionException>();
}

[Fact]
public async Task Handle_NewSubscriber_ShouldCreateSubscriberAndSaveChanges()
{
// Arrange
var dbContext = _fixture.BuildDbContext(Guid.NewGuid().ToString());
var subscribeRepository = new SubscriberRepository(dbContext);

var subscriberId = SubscriberId.CreateUniqueId("[email protected]");

var subscriberService = new SubscriberService(subscribeRepository);

var sut = new SubscribeCommandHandler(subscribeRepository, subscriberService);

var command = new SubscribeCommand(subscriberId);

// Act
await sut.Handle(command, CancellationToken.None);

// Assert
var subscriber = await subscribeRepository.FindByIdAsync(subscriberId);
subscriber.Should().NotBeNull();
}
}

0 comments on commit 92041c5

Please sign in to comment.