【问题标题】:XUnit unit tests for MassTransit consumerMassTransit 消费者的 XUnit 单元测试
【发布时间】:2019-08-29 23:11:05
【问题描述】:

我正在使用 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);

【问题讨论】:

  • 什么不起作用?预期行为和实际行为是什么?
  • 我没有正确地模拟对象。
  • 改用内存中的测试工具,模拟是失败的,因为它不会真正为您提供对消息/消费者组合的真正测试。在此处查看示例:github.com/MassTransit/MassTransit/blob/develop/src/…
  • 再加上 Chris 写的内容。使用 MassTransit 测试工具,请不要使用模拟。

标签: c# moq xunit masstransit consumer


【解决方案1】:

由于您还没有弄清楚实际上什么不起作用,我能提供的最多就是如何创建模拟上下文并将其传递给被测对象方法。

这很简单,因为ConsumeContext&lt;T&gt; 已经是一个接口

[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

【讨论】:

    猜你喜欢
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多