用于MassTransit消费者的XUnit单元测试

9

我正在使用 MassTransit 5.5.5 版本和 xunit 2.4.1。

我的消费者类看起来像这样:

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)
    {
        //....
    }
}

而我的测试如下所示

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
    }
}

如何使用CreateStorageInformationSummary消息以便调用消费者,以下方法不起作用。
var mockMessage = new Mock<ConsumeContext<CreateStorageInformationSummary>>(createStorageInformationSummary);
await _storageInformationConsumer.Consume(mockMessage.Object);

什么不能正常工作?期望的行为和实际的行为分别是什么? - Nkosi
我没有正确地模拟对象。 - user1532976
1
请使用内存测试工具代替模拟,因为模拟无法真正为消息/消费者组合提供真实的测试。在此处查看示例:https://github.com/MassTransit/MassTransit/blob/develop/src/MassTransit.Tests/Testing/ConsumerTest_Specs.cs - Chris Patterson
在Chris所写的基础上再加一点,使用MassTransit测试工具,不要使用mock。 - Alexey Zimarev
1个回答

11

由于您没有明确说明实际上出了什么问题,我能提供的最多只是如何创建模拟上下文并将其传递给测试中的主题方法。

这很简单,因为ConsumeContext<T>已经是一个接口。

[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
}

请注意测试已更新为返回async Task而不是async void

参考Moq Quickstart


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接