Adds Verify support for MassTransit test helpers.
Перейти к файлу
dependabot[bot] 3861035535
Bump Verify from 16.3.2 to 16.3.3
Bumps [Verify](https://github.com/VerifyTests/Verify) from 16.3.2 to 16.3.3.
- [Release notes](https://github.com/VerifyTests/Verify/releases)
- [Commits](https://github.com/VerifyTests/Verify/compare/16.3.2...16.3.3)

---
updated-dependencies:
- dependency-name: Verify
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-03-01 23:46:24 +00:00
.github . 2022-02-25 22:19:33 +11:00
src Bump Verify from 16.3.2 to 16.3.3 2022-03-01 23:46:24 +00:00
.gitignore . 2022-02-25 22:19:33 +11:00
license.txt . 2022-02-25 22:19:33 +11:00
readme.md . 2022-02-26 16:35:01 +11:00

readme.md

Verify.MassTransit

Build status NuGet Status

Adds Verify support to verify MassTransit test helpers.

NuGet package

https://nuget.org/packages/Verify.MassTransit/

Usage

Before any test have run call:

public static class ModuleInitializer
{
    [ModuleInitializer]
    public static void Initialize()
    {
        VerifyMassTransit.Enable();
    }
}

snippet source | anchor

Consumer Test

Using traditional asserts consumer interactions can be tested as follows:

[Fact]
public async Task TestWithAsserts()
{
    var harness = new InMemoryTestHarness();
    var consumerHarness = harness.Consumer<SubmitOrderConsumer>();

    await harness.Start();
    try
    {
        await harness.InputQueueSendEndpoint.Send<SubmitOrder>(
            new
            {
                OrderId = InVar.Id
            });

        // did the endpoint consume the message
        Assert.True(await harness.Consumed.Any<SubmitOrder>());
        // did the actual consumer consume the message
        Assert.True(await consumerHarness.Consumed.Any<SubmitOrder>());
        // the consumer publish the event
        Assert.True(await harness.Published.Any<OrderSubmitted>());
        // ensure that no faults were published by the consumer
        Assert.False(await harness.Published.Any<Fault<SubmitOrder>>());
    }
    finally
    {
        await harness.Stop();
    }
}

snippet source | anchor

Using Verify, the TestHarness and any number of ConsumerHarness, can be passed to Verify.

[Fact]
public async Task TestWithVerify()
{
    var harness = new InMemoryTestHarness();
    var consumer = harness.Consumer<SubmitOrderConsumer>();

    await harness.Start();
    try
    {
        await harness.InputQueueSendEndpoint
            .Send<SubmitOrder>(
                new
                {
                    OrderId = InVar.Id
                });

        await Verify(new {harness, consumer});
    }
    finally
    {
        await harness.Stop();
    }
}

snippet source | anchor

The above will result in the following snapshot file that will need to be accepted.

{
  harness: {
    Messages: [
      {
        Sent: ConsumerTests.SubmitOrder,
        MessageId: Guid_1,
        ConversationId: Guid_2,
        DestinationAddress: input_queue,
        Message: {
          OrderId: Guid_3
        }
      },
      {
        Received: ConsumerTests.SubmitOrder,
        MessageId: Guid_1,
        ConversationId: Guid_2,
        DestinationAddress: input_queue,
        Message: {
          OrderId: Guid_3
        }
      },
      {
        Published: ConsumerTests.OrderSubmitted,
        MessageId: Guid_4,
        ConversationId: Guid_2,
        DestinationAddress: Tests:ConsumerTests+OrderSubmitted,
        Message: {
          OrderId: Guid_3
        }
      }
    ]
  },
  consumer: {
    Consumed: [
      {
        Received: ConsumerTests.SubmitOrder,
        MessageId: Guid_1,
        ConversationId: Guid_2,
        DestinationAddress: input_queue,
        Message: {
          OrderId: Guid_3
        }
      }
    ]
  }
}

snippet source | anchor

Moving forward, any change in the message interactions will result in a new snapshot that can then be accepted or declines

Saga Test

The following Saga test:

[Fact]
public async Task Run()
{
    var harness = new InMemoryTestHarness();
    var sagaHarness = harness.Saga<ConsumerSaga>();

    var correlationId = NewId.NextGuid();

    await harness.Start();
    try
    {
        await harness.Bus.Publish(new Start {CorrelationId = correlationId});

        await harness.Consumed.Any<Start>();

        await Verify(new {harness, sagaHarness});
    }
    finally
    {
        await harness.Stop();
    }
}

public class ConsumerSaga :
    ISaga,
    InitiatedBy<Start>
{
    public Guid CorrelationId { get; set; }
    public bool StartMessageReceived { get; set; }

    public Task Consume(ConsumeContext<Start> context)
    {
        StartMessageReceived = true;
        return Task.CompletedTask;
    }
}

public class Start : CorrelatedBy<Guid>
{
    public Guid CorrelationId { get; set; }
}

snippet source | anchor

Will result in the following snapshot file.

{
  harness: {
    Messages: [
      {
        Published: SagaTests.Start,
        MessageId: Guid_1,
        ConversationId: Guid_2,
        DestinationAddress: Tests:SagaTests+Start,
        Message: {
          CorrelationId: Guid_3
        }
      },
      {
        Received: SagaTests.Start,
        MessageId: Guid_1,
        ConversationId: Guid_2,
        DestinationAddress: Tests:SagaTests+Start,
        Message: {
          CorrelationId: Guid_3
        }
      }
    ]
  },
  sagaHarness: {
    Consumed: [
      {
        Received: SagaTests.Start,
        MessageId: Guid_1,
        ConversationId: Guid_2,
        DestinationAddress: Tests:SagaTests+Start,
        Message: {
          CorrelationId: Guid_3
        }
      }
    ],
    Sagas: [
      {
        Saga: {
          CorrelationId: Guid_3,
          StartMessageReceived: true
        },
        ElementId: Guid_3
      }
    ]
  }
}

snippet source | anchor

Icon

Approval designed by SAM Designs from The Noun Project.