如何验证NServiceBus Handler中的多个操作

时间:2013-06-04 20:31:45

标签: .net unit-testing nservicebus

我有一个处理程序需要在从存储库检索的实体的每个实例上发送本地消息。传递给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>();
}

1 个答案:

答案 0 :(得分:1)

这看起来像个bug,你介意为此提出一个github问题吗?