Skip to content

Instantly share code, notes, and snippets.

@explorer14
Last active Jun 21, 2020
Embed
What would you like to do?
[Fact]
public async Task ShouldPublishNotificationForSuccessfulRegistration()
{
var customerRepositoryStub = CreateEmptyRepository();
// Notice, I don't really care about the values in the request
// because I am testing the collaboration with the event bus
var testCustomerRegistrationRequest = CreateTestRequest();
var registrationEvent = default(NewCustomerRegistered);
// Notice the use of a different kind of test double here - a SPY!
var eventBusSpy = new Mock<IEventBus>();
eventBusSpy
.Setup(x=>x.Publish(It.IsAny<NewCustomerRegistered>()))
.ReturnsAsync(Task.CompletedTask)
.Callback<NewCustomerRegistered>(payload=> registrationEvent = payload);
// Create the system under test with a stub and a spy
var useCase = new CustomerRegistrationUseCase(
customerRepositoryStub,
eventBusSpy.Object);
await useCase.RegisterCustomer(testCustomerRegistrationRequest);
registrationEvent.Should().NotBeNull();
registrationEvent.Email.Should().Be(testCustomerRegistrationRequest.Email);
registrationEvent.FirstName.Should().Be(testCustomerRegistrationRequest.FirstName);
registrationEvent.LastName.Should().Be(testCustomerRegistrationRequest.LastName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment