autofac - 在 xunit 测试中触发 MassTransit 消费者时遇到问题
问题描述
我在尝试对一些使用 MassTransit 传奇状态机和消费者的代码进行单元测试时遇到了 2 个问题。我使用 Autofac 作为容器,使用 xUnit 作为测试框架。
我在这里有一个示例单元测试来说明这两个问题。
当只使用 Autofac 容器来设置我的 SagaStateMachine 和 Consumer 时,我不能断言我的状态机或消费者已经消费了任何消息 - 好像它们没有连接到测试总线。
(测试ExampleTest_AutofacOnly
)我可以通过在测试中引入 a并从而不是
ServiceCollection
解决线束来解决这个问题,但这感觉像是双重处理。 (测试)ServiceProvider
IContainer
ExampleTest_AutofacAndServiceProvider
我无法使用
InMemoryTestHarness.Bus.Publish()
(但我可以触发状态机)触发我的消费者。真正让人恼火的是,我可以使用 触发我的状态机InMemoryTestHarness.Bus.Publish()
,然后状态机在处理事件时可以触发消费者。
(测试ExampleTest2_AutofacOnly
&ExampleTest2_AutofacAndServiceProvider
)
编辑
(2) 在我注释掉一些行之后,似乎不再是问题了。我的示例中的等效行是:
// In my actual unit test the code below causes the test to fail
var state = sagaHarness.Sagas.Select(s => s.CorrelationId == correlationId).Single();
Assert.Equal("Progress state machine", state.Saga.Message);
// 4. Trigger the second consumer
await testHarness.Bus.Publish(new ExampleMessage2
{
CorrelationId = correlationId,
Message = "Trigger consumer",
});
// 5. Check that ExampleMessage has been published and received
Assert.True(await testHarness.Published.Any<ExampleMessage2>(m => m.Context.CorrelationId == correlationId));
Assert.True(await testHarness.Consumed.Any<ExampleMessage2>(m => m.Context.CorrelationId == correlationId));
Assert.True(await consumer2Harness.Consumed.Any<ExampleMessage2>(m => m.Context.CorrelationId == correlationId));
当我得到 saga 实例时,sagaHarness.Sagas.Select()
它会阻止在它下面发布的消息被使用。
解决方案
推荐阅读
- pdf - 如何从外部文件自动添加/替换现有 PDF 中的文本?
- javascript - 让 div 跟随光标在特定的 div 父级中
- python - 获取具有不同长度列表的列的稀疏 df 并确保正确合并
- mysql - 加入具有重复行数据的表时如何获得正确的 SUM()?
- laravel - limit() 和 take() 不能正常工作
- javascript - Material UI - React Js - Datepicker 选择下一个日期
- laravel - Laravel 和 axios 安装在其他子域 CORS 问题上
- php - 使用 Laravel 跳过一些行的自定义报告格式
- android - 不允许在 MAUI Blazor 中加载本地资源
- flutter - 从 pubspec.yaml 检索应用程序的当前版本