这应该很简单,我可能遗漏了一些东西。我在这里工作:http: //support.nservicebus.com/customer/portal/articles/856297-unit-testing
以下总是失败:
[TestFixture]
public class MyTestFixture
{
[Test]
public void RoundTrip() {
Test.Initialize();
var correlationId = Guid.NewGuid();
Test.Handler(bus => new MyHandler(bus))
.ExpectReply<IEventThatHappened>(m => m.CorrelationId == correlationId)
.OnMessage<MyCommand>(m => new MyCommand(correlationId));
}
}
public interface IEventThatHappened : IEvent
{
Guid CorrelationId { get; set; }
}
public class MyCommand : ICommand
{
public Guid CorrelationId { get; private set; }
public MyCommand(Guid correlationId) {
CorrelationId = correlationId;
}
}
public class MyHandler : IHandleMessages<MyCommand>
{
private readonly IBus _bus;
public MyHandler(IBus bus) {
if (bus == null) {
throw new ArgumentNullException("bus");
}
_bus = bus;
}
public void Handle(MyCommand message) {
_bus.Send<IEventThatHappened>(m => m.CorrelationId = message.CorrelationId);
}
}
如果我在处理程序中设置断点,则 message.CorrelationId == Guid.Empty。测试过程中抛出的异常是:
System.Exception:ExpectedReplyInvocation 未完成。调用:SendInvocation
我尝试过使用 bus.Send、bus.Publish、bus.Reply ,但每一个都因相应的 Expected*Invocation 而失败。
为什么是 message.CorrelationId == Guid.Empty 而不是我提供的值?为什么 Test.Handler<> 没有检测到我在处理程序中调用了 Send/Reply/Publish?
注意:使用 Nuget 的 NServiceBus 3.3。