Skip to content

Commit

Permalink
Fix assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleMcMaster committed Apr 14, 2024
1 parent e8a600a commit 9b41789
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@

namespace NServiceBusTutorial.Worker;

public class ContributorCreateCommandHandler(IRepository<Contributor> _repository) : IHandleMessages<ContributorCreateCommand>
public class ContributorCreateCommandHandler(IRepository<Contributor> _repository)
: IHandleMessages<ContributorCreateCommand>
{
public async Task Handle(ContributorCreateCommand message, IMessageHandlerContext context)
{
var phoneNumber = new PhoneNumber(string.Empty, message.PhoneNumber, string.Empty);
var contributor = new Contributor(message.Name, phoneNumber, ContributorStatus.NotSet);
await _repository.AddAsync(contributor, context.CancellationToken);
var created = await _repository.AddAsync(contributor, context.CancellationToken);

await context.Publish(new ContributorCreatedEvent
{
ContributorId = contributor.Id,
ContributorId = created.Id,
Name = contributor.Name,
Status = contributor.Status.ToString()
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ public async Task ShouldPublishContributorCreatedEvent()
Name = "Test Contributor",
PhoneNumber = "123-456-7890"
};
var context = new TestableMessageHandlerContext();
var phoneNumber = new PhoneNumber(string.Empty, message.PhoneNumber, string.Empty);
var contributor = new Contributor(message.Name, phoneNumber, ContributorStatus.NotSet)
{
Id = 1
};
var repository = Substitute.For<IRepository<Contributor>>();
repository
.AddAsync(Arg.Any<Contributor>(), Arg.Any<CancellationToken>())
.Returns(contributor);
var context = new TestableMessageHandlerContext();
var handler = new ContributorCreateCommandHandler(repository);

await handler.Handle(message, context);

using var assertionScope = new AssertionScope();
var publishedMessage = context.PublishedMessages.Single();
publishedMessage.Should().BeOfType<ContributorCreatedEvent>();
publishedMessage.Message.As<ContributorCreatedEvent>().ContributorId.Should().Be(1);
publishedMessage.As<ContributorCreatedEvent>().Name.Should().Be(message.Name);
publishedMessage.As<ContributorCreatedEvent>().Status.Should().Be(ContributorStatus.NotSet.Name);
}
Expand Down

0 comments on commit 9b41789

Please sign in to comment.