NServiceBus 单元测试:bus.Send() 时 ExpectSend 未实现
NServiceBus unit testing: ExpectSend not fulfilled when bus.Send()
我正在使用 NServiceBus5.2.21、NServiceBus.Testing5.2.1、Moq4.0.1、NUnit3.2.0
问题:ExpectSend<>
一直失败
"ExpectedSendInvocation not fulfilled"
当 _bus.Send()
被正确调用时。不确定我在这里遗漏了什么。
测试:
[TestFixture]
public class GeneralLedgerHanlderTest
{
private Mock<ISendGeneralLedgerToSap> _toSapMock;
[SetUp]
public void Setup()
{
_toSapMock = new Mock<ISendGeneralLedgerToSap>();
_toSapMock.Setup(x => x.SendSoapMessageToSap(It.IsAny<GeneralLedgerSapCommand>()));
}
[Test]
public void HandlerMustSendAuditLog()
{
NServiceBus.Testing.Test.Handler(bus => new GeneralLedgerToSapHandler(bus, _toSapMock.Object))
.ExpectSend<GeneralLedgerAuditCommand>(command => command.SagaReferenceId == "ab")
.OnMessage(new GeneralLedgerSapCommand { SagaReferenceId = "ab" });
}
}
处理程序:
public class GeneralLedgerToSapHandler : IHandleMessages<GeneralLedgerSapCommand>
{
private readonly IBus _bus;
private readonly ISendGeneralLedgerToSap _sendSoapToSap;
public GeneralLedgerToSapHandler(IBus bus, ISendGeneralLedgerToSap sendSoapToSap)
{
_bus = bus;
_sendSoapToSap = sendSoapToSap;
}
public void Handle(GeneralLedgerSapCommand message)
{
_sendSoapToSap.SendSoapMessageToSap(message);
var goodsReceiptAuditCommand = new GeneralLedgerAuditCommand
{
SagaReferenceId = message.SagaReferenceId,
};
_bus.Send(EndPointName.SpmAuditLogService, goodsReceiptAuditCommand);
}
}
这有点难以理解,但问题是由于您使用发送:
_bus.Send(EndPointName.SpmAuditLogService, goodsReceiptAuditCommand);
您要发送到特定目的地(我建议改用路由)。
测试框架有一个特定的 "Expect" 选项用于发送到特定的目的地。如果您改用 ExpectSendToDestination
,您的测试应该可以正常工作:
.ExpectSendToDestination<GeneralLedgerAuditCommand>((command, destination) => command.SagaReferenceId == "ab")
在NServiceBus.Testing v6(依赖于NServiceBus v6)中,这个问题已经修复,ExpectSend
发送到特定目的地时也会被调用。
编辑:我在 NServiceBus.Testing 中提出了一个关于这种令人困惑的行为的问题,请参阅 https://github.com/Particular/NServiceBus.Testing/issues/122
我正在使用 NServiceBus5.2.21、NServiceBus.Testing5.2.1、Moq4.0.1、NUnit3.2.0
问题:ExpectSend<>
一直失败
"ExpectedSendInvocation not fulfilled"
当 _bus.Send()
被正确调用时。不确定我在这里遗漏了什么。
测试:
[TestFixture]
public class GeneralLedgerHanlderTest
{
private Mock<ISendGeneralLedgerToSap> _toSapMock;
[SetUp]
public void Setup()
{
_toSapMock = new Mock<ISendGeneralLedgerToSap>();
_toSapMock.Setup(x => x.SendSoapMessageToSap(It.IsAny<GeneralLedgerSapCommand>()));
}
[Test]
public void HandlerMustSendAuditLog()
{
NServiceBus.Testing.Test.Handler(bus => new GeneralLedgerToSapHandler(bus, _toSapMock.Object))
.ExpectSend<GeneralLedgerAuditCommand>(command => command.SagaReferenceId == "ab")
.OnMessage(new GeneralLedgerSapCommand { SagaReferenceId = "ab" });
}
}
处理程序:
public class GeneralLedgerToSapHandler : IHandleMessages<GeneralLedgerSapCommand>
{
private readonly IBus _bus;
private readonly ISendGeneralLedgerToSap _sendSoapToSap;
public GeneralLedgerToSapHandler(IBus bus, ISendGeneralLedgerToSap sendSoapToSap)
{
_bus = bus;
_sendSoapToSap = sendSoapToSap;
}
public void Handle(GeneralLedgerSapCommand message)
{
_sendSoapToSap.SendSoapMessageToSap(message);
var goodsReceiptAuditCommand = new GeneralLedgerAuditCommand
{
SagaReferenceId = message.SagaReferenceId,
};
_bus.Send(EndPointName.SpmAuditLogService, goodsReceiptAuditCommand);
}
}
这有点难以理解,但问题是由于您使用发送:
_bus.Send(EndPointName.SpmAuditLogService, goodsReceiptAuditCommand);
您要发送到特定目的地(我建议改用路由)。
测试框架有一个特定的 "Expect" 选项用于发送到特定的目的地。如果您改用 ExpectSendToDestination
,您的测试应该可以正常工作:
.ExpectSendToDestination<GeneralLedgerAuditCommand>((command, destination) => command.SagaReferenceId == "ab")
在NServiceBus.Testing v6(依赖于NServiceBus v6)中,这个问题已经修复,ExpectSend
发送到特定目的地时也会被调用。
编辑:我在 NServiceBus.Testing 中提出了一个关于这种令人困惑的行为的问题,请参阅 https://github.com/Particular/NServiceBus.Testing/issues/122