From 9b4178922e3b6910e7500e637ed5c50f99e0e64d Mon Sep 17 00:00:00 2001 From: Kyle McMaster Date: Sun, 14 Apr 2024 17:50:19 -0400 Subject: [PATCH] Fix assertions --- .../ContributorCreateCommandHandler.cs | 7 ++++--- .../ContributorCreateCommandHandlerTests/Handle.cs | 11 ++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/NServiceBusTutorial.Worker/ContributorCreateCommandHandler.cs b/src/NServiceBusTutorial.Worker/ContributorCreateCommandHandler.cs index 3d5a442..52e0d94 100644 --- a/src/NServiceBusTutorial.Worker/ContributorCreateCommandHandler.cs +++ b/src/NServiceBusTutorial.Worker/ContributorCreateCommandHandler.cs @@ -6,17 +6,18 @@ namespace NServiceBusTutorial.Worker; -public class ContributorCreateCommandHandler(IRepository _repository) : IHandleMessages +public class ContributorCreateCommandHandler(IRepository _repository) + : IHandleMessages { 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() }); diff --git a/tests/NServiceBusTutorial.UnitTests/ContributorCreateCommandHandlerTests/Handle.cs b/tests/NServiceBusTutorial.UnitTests/ContributorCreateCommandHandlerTests/Handle.cs index 0cdd5ca..11a0df7 100644 --- a/tests/NServiceBusTutorial.UnitTests/ContributorCreateCommandHandlerTests/Handle.cs +++ b/tests/NServiceBusTutorial.UnitTests/ContributorCreateCommandHandlerTests/Handle.cs @@ -21,8 +21,16 @@ 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>(); + repository + .AddAsync(Arg.Any(), Arg.Any()) + .Returns(contributor); + var context = new TestableMessageHandlerContext(); var handler = new ContributorCreateCommandHandler(repository); await handler.Handle(message, context); @@ -30,6 +38,7 @@ public async Task ShouldPublishContributorCreatedEvent() using var assertionScope = new AssertionScope(); var publishedMessage = context.PublishedMessages.Single(); publishedMessage.Should().BeOfType(); + publishedMessage.Message.As().ContributorId.Should().Be(1); publishedMessage.As().Name.Should().Be(message.Name); publishedMessage.As().Status.Should().Be(ContributorStatus.NotSet.Name); }