XUnit unit tests for MassTransit consumer
Asked Answered
L

1

10

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);
Lonnalonnard answered 29/8, 2019 at 23:11 Comment(4)
What doesn't work? What is the expected behavior and actual behavior?Vend
I was not mocking object correctly.Lonnalonnard
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: github.com/MassTransit/MassTransit/blob/develop/src/…Spanking
Plus one on what Chris wrote. Use the MassTransit test harness, don't use mocks please.Postoperative
V
12

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

Vend answered 29/8, 2019 at 23:35 Comment(2)
Well, Message has no setter at least in my Mock version... Not working. Also you put == and guess you meant =.Yonne
@VedranMandić The expression used in the mock is a predicate and not meant to assign an actual value hence the ==Vend

© 2022 - 2024 — McMap. All rights reserved.