7

I am using MassTransit 5.5.5 version and xunit 2.4.1

My consumer looks like this

public class StorageInformationConsumer : IConsumer<CreateStorageInformationSummary>
{
    private readonly IBus _serviceBus;
    private readonly USIntegrationQueueServiceContext _USIntegrationQueueServiceContext;


    public StorageInformationConsumer(IBus serviceBus, USIntegrationQueueServiceContext USIntegrationQueueServiceContext)
    {
        _serviceBus = serviceBus;
        _USIntegrationQueueServiceContext = USIntegrationQueueServiceContext;
    }

    public async Task Consume(ConsumeContext<CreateStorageInformationSummary> createStorageInformationSummarycontext)
    {
        //....
    }
}

And my Test like this

public class StorageInformationConsumerTest
{
    private readonly USIntegrationQueueServiceContext _dbContext;
    private readonly Mock<IBus> _serviceBusMock;
    private readonly StorageInformationConsumer _storageInformationConsumer;

    public StorageInformationConsumerTest()
    {
        var options = new DbContextOptionsBuilder<USIntegrationQueueServiceContext>()
                    .UseInMemoryDatabase(databaseName: "InMemoryArticleDatabase")
                    .Options;
        _dbContext = new USIntegrationQueueServiceContext(options);
        _serviceBusMock = new Mock<IBus>();
        _storageInformationConsumer = new StorageInformationConsumer(_serviceBusMock.Object, _dbContext);
    }

    [Fact]
    public async void ItShouldCreateStorageInformation()
    {
        var createStorageInformationSummary = new CreateStorageInformationSummary
        {
            ClaimNumber = "C-1234",
            WorkQueueItemId = 1,
            StorageInformation = CreateStorageInformation(),
        };

        //How to consume
    }
}

How to consume the CreateStorageInformationSummary message in order to call consumer, following doesn't work

var mockMessage = new Mock<ConsumeContext<CreateStorageInformationSummary>>(createStorageInformationSummary);
await _storageInformationConsumer.Consume(mockMessage.Object);
Nkosi
  • 235,767
  • 35
  • 427
  • 472
user1532976
  • 469
  • 2
  • 8
  • 15
  • What doesn't work? What is the expected behavior and actual behavior? – Nkosi Aug 29 '19 at 23:27
  • I was not mocking object correctly. – user1532976 Aug 29 '19 at 23:40
  • 1
    Use the in-memory test harness instead, mocking is a fail since it won't really give you a true test on the message/consumer combination. See an example here: https://github.com/MassTransit/MassTransit/blob/develop/src/MassTransit.Tests/Testing/ConsumerTest_Specs.cs – Chris Patterson Aug 30 '19 at 01:39
  • Plus one on what Chris wrote. Use the MassTransit test harness, don't use mocks please. – Alexey Zimarev Aug 30 '19 at 06:38

1 Answers1

10

Since you have not clarified what is actually not working, the most I can provide is how to create the mock context and pass it to the subject method under test.

This is simple enough since ConsumeContext<T> is already an interface

[Fact]
public async Task ItShouldCreateStorageInformation() {
    //Arrange
    var createStorageInformationSummary = new CreateStorageInformationSummary {
        ClaimNumber = "C-1234",
        WorkQueueItemId = 1,
        StorageInformation = CreateStorageInformation(),
    };
    //Mock the context
    var context = Mock.Of<ConsumeContext<CreateStorageInformationSummary>>(_ => 
        _.Message == createStorageInformationSummary);

    //Act
    await _storageInformationConsumer.Consume(context);

    //Assert
    //...assert the expected behavior
}

Also take note that the test has been updated to return async Task and not async void

Reference Moq Quickstart

Nkosi
  • 235,767
  • 35
  • 427
  • 472