当 Saga 有多个处理程序时,如何为 NServiceBus Saga 创建单元测试
我曾经做过这个测试,它通过得很好(一旦处理了所有三个消息,传奇就完成了。
Test.Saga<TestSagaHandler>(sagaId)
.When(x =>
{
x.Handle(new TestSagaStartMessageOne
{
Id = sagaId
});
x.Handle(new TestSagaStartMessageTwo
{
Id = sagaId
});
x.Handle(new TestSagaNonStartingMessage
{
Id = sagaId
});
});
.AssertSagaCompletionIs(true);
我现在想将 TestSagaNonStartingMessage 分解到它自己的处理程序中,并执行以下操作:
Test.Saga<TestSagaHandler>(sagaId)
.When(x =>
{
x.Handle(new TestSagaStartMessageOne
{
Id = sagaId
});
x.Handle(new TestSagaStartMessageTwo
{
Id = sagaId
});
});
Test.Saga<TestSagaHandlerSingleMessage>(sagaId)
.When(x =>
x.Handle(new TestSagaNonStartingMessage
{
Id = sagaId
})
)
.AssertSagaCompletionIs(true);
但是,在处理 TestSagaNonStartingMessage 时 - saga 数据没有从以前的处理程序中持久化。
我是否存在持久性问题,或者测试构建得不好?
I used to have this test, which passes just fine (The saga is complete once all three messages have been handled.
Test.Saga<TestSagaHandler>(sagaId)
.When(x =>
{
x.Handle(new TestSagaStartMessageOne
{
Id = sagaId
});
x.Handle(new TestSagaStartMessageTwo
{
Id = sagaId
});
x.Handle(new TestSagaNonStartingMessage
{
Id = sagaId
});
});
.AssertSagaCompletionIs(true);
I now want to break out the TestSagaNonStartingMessage into its own handler, and did the following:
Test.Saga<TestSagaHandler>(sagaId)
.When(x =>
{
x.Handle(new TestSagaStartMessageOne
{
Id = sagaId
});
x.Handle(new TestSagaStartMessageTwo
{
Id = sagaId
});
});
Test.Saga<TestSagaHandlerSingleMessage>(sagaId)
.When(x =>
x.Handle(new TestSagaNonStartingMessage
{
Id = sagaId
})
)
.AssertSagaCompletionIs(true);
However, when handling the TestSagaNonStartingMessage - the saga data is not persisted from the previous handlers.
Am I having persistence problems, or is the test constructed badly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试构建不正确 - 请查看制造示例中的测试项目以了解应如何构建它。简短的答案是将第二个 .When(...) 链接在第一个之后。
The test isn't constructed correctly - please look at the test project in the manufacturing sample to see how it should be structured. The short answer is to chain the second .When(...) after the first.
对于其他读者参考,正确的测试结构应类似于:
正如 Udi 所示,将“When”子句链接在一起。
另外,为了从测试中获得更多价值,请考虑引入异常,例如
ExpectSend<>
、ExpectPublish
等。参考:http://docs.pspecial.net/nservicebus/testing/
For other readers reference, the proper test structure should be similar to:
As Udi indicated, chain the "When" clauses together.
Also, in order to derive further value from the test, consider introducing exceptions, such as
ExpectSend<>
,ExpectPublish
, etc.Reference: http://docs.particular.net/nservicebus/testing/