Testing NServiceBus with ApprovalTests

Source
NuGet Package NServiceBus.ApprovalTests (1.x) | License
This is a community maintained project. License and support are independent of Particular Software.
Target NServiceBus Version: 7.x

NServiceBus.ApprovalTests adds support for using ApprovalTests to verify NServiceBus Test Contexts.

Verifying a context

Given the following handler:

public class MyHandler :
    IHandleMessages<MyRequest>
{
    public async Task Handle(MyRequest message, IMessageHandlerContext context)
    {
        await context.Publish(
                new MyPublishMessage
                {
                    Property = "Value"
                })
            .ConfigureAwait(false);

        await context.Reply(
                new MyReplyMessage
                {
                    Property = "Value"
                })
            .ConfigureAwait(false);

        var sendOptions = new SendOptions();
        sendOptions.DelayDeliveryWith(TimeSpan.FromHours(12));
        await context.Send(
                new MySendMessage
                {
                    Property = "Value"
                },
                sendOptions)
            .ConfigureAwait(false);

        await context.ForwardCurrentMessageTo("newDestination")
            .ConfigureAwait(false);
    }
}

The test that verifies the resulting context:

[Test]
public async Task VerifyHandlerResult()
{
    var handler = new MyHandler();
    var context = new TestableMessageHandlerContext();

    await handler.Handle(new MyRequest(), context)
        .ConfigureAwait(false);

    TestContextVerifier.Verify(context);
}

The resulting context verification file is as follows:

{
  "RepliedMessages": [
    {
      "MyReplyMessage": {
        "Property": "Value"
      }
    }
  ],
  "ForwardedMessages": [
    "newDestination"
  ],
  "MessageId": "Guid 1",
  "ReplyToAddress": "reply address",
  "SentMessages": [
    {
      "MySendMessage": {
        "Property": "Value"
      },
      "Options": {
        "DeliveryDelay": "12:00:00"
      }
    }
  ],
  "PublishedMessages": [
    {
      "MyPublishMessage": {
        "Property": "Value"
      }
    }
  ],
  "Extensions": {}
}

Example behavior change

The next time there is a code change, that results in a different resulting interactions with NServiceBus, those changes can be visualized. For example if the DelayDeliveryWith is changed from 12 hours to 1 day:

await context.Publish(
        new MyPublishMessage
        {
            Property = "Value"
        })
    .ConfigureAwait(false);

await context.Reply(
        new MyReplyMessage
        {
            Property = "Value"
        })
    .ConfigureAwait(false);

var sendOptions = new SendOptions();
sendOptions.DelayDeliveryWith(TimeSpan.FromDays(1));
await context.Send(
        new MySendMessage
        {
            Property = "Value"
        },
        sendOptions)
    .ConfigureAwait(false);

await context.ForwardCurrentMessageTo("newDestination")
    .ConfigureAwait(false);

Then the resulting visualization diff would look as follows:

visualization diff


Last modified