我有一个处理程序,需要在从存储库中检索到的实体的每个实例上发送本地消息。传递给的回调仅在我的处理程序中NServiceBus.Testing.Handler<>::ExpectSendLocal()
的第一次调用时被调用。IBus::SendLocal()
我试过链接第二个Handler<>::ExpectSendLocal()
,但它的回调也只在第一个上调用。这是一个示例处理程序:
public FooHandler : IHandleMessages<IFoo>
{
public void Handle(IFoo message)
{
var bars = new [] {new Bar {Zap = "Zap1"}, new Bar {Zap = "Zap2"}};
foreach (var bar in bars)
{
this.Bus().SendLocal<IBarProcessed>(barMsg => {
barMsg.Zap = bar.Zap;
});
bar.IsProcessed = true;
}
}
}
IBus::SendLocal()
这是一个增加期望计数的示例单元测试:
public void When_IFoo_message_received()
{
int actualCount = 0;
new NServiceBus.Testing.Handler<FooHandler>()
.ExpectSendLocal<IBarProcessed>(completed => actualCount++)
.OnMessage<IFoo>();
Assert.AreEqual(2, actualCount); // fails because actualCount is one
}
这是一个示例单元测试,它链接NServiceBus.Testing.Handler<FooHandler>.ExpectSendLocal()
并检查来自 2 条消息的 2 个不同值:
public void When_IFoo_message_received()
{
new NServiceBus.Testing.Handler<FooHandler>()
.ExpectSendLocal<IBarProcessed>(completed => {
Assert.AreEqual("Zap1", completed.Zap);
})
.ExpectSendLocal<IBarProcessed>(completed => {
Assert.AreEqual("Zap2", completed.Zap); // fails because it's the value from the first one e.g. "Zap1"
})
.OnMessage<IFoo>();
}