C# MassTransit用户的XUnit单元测试

C# MassTransit用户的XUnit单元测试,c#,moq,xunit,masstransit,consumer,C#,Moq,Xunit,Masstransit,Consumer,我使用的是MassTransit 5.5.5版本和xunit 2.4.1 我的消费者看起来像这样 public class StorageInformationConsumer : IConsumer<CreateStorageInformationSummary> { private readonly IBus _serviceBus; private readonly USIntegrationQueueServiceContext _USIntegrationQ

我使用的是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
    }
}

因为您还没有澄清什么实际上不起作用,所以我所能提供的最多就是如何创建模拟上下文并将其传递给测试中的subject方法

这非常简单,因为
consumercontext
已经是一个接口

[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
}
[事实]
公共异步任务ItShouldCreateStorageInformation(){
//安排
var createStorageInformationSummary=新建createStorageInformationSummary{
ClaimNumber=“C-1234”,
WorkQueueItemId=1,
StorageInformation=CreateStorageInformation(),
};
//嘲弄上下文
var context=Mock.Of(=>
_.Message==createStorageInformationSummary);
//表演
wait_storageInformationConsumer.consumer(上下文);
//断言
//…断言预期的行为
}
还要注意,测试已更新为返回
async Task
,而不是
async void


参考资料

什么不起作用?预期行为和实际行为是什么?我没有正确模拟对象。请改用内存中的测试工具,模拟是失败的,因为它不会真正对消息/使用者组合进行真正的测试。这里有一个例子:加上Chris写的。使用MassTransit测试线束,请不要使用模拟。
[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
}