1

我正在使用 NServiceBus5.2.21、NServiceBus.Testing5.2.1、Moq4.0.1、NUnit3.2.0

问题:ExpectSend<>一直失败

“ExpectedSendInvocation 未完成”

何时_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);
    }
}
4

1 回答 1

2

这有点难以弄清楚,但是由于您使用了发送而出现了问题:

_bus.Send(EndPointName.SpmAuditLogService, goodsReceiptAuditCommand);

您正在发送到特定目的地(我建议您改用路由)。

测试框架具有用于发送到特定目的地的特定“期望”选项。如果您改用以下方法,您的测试应该可以正常工作ExpectSendToDestination

.ExpectSendToDestination<GeneralLedgerAuditCommand>((command, destination) => command.SagaReferenceId == "ab")

在 NServiceBus.Testing v6(依赖于 NServiceBus v6)中,这已得到修复,并且ExpectSend在发送到特定目的地时也会被调用。

编辑:我在NServiceBus.Testing中提出了关于这种令人困惑的行为的问题,请参阅https://github.com/Particular/NServiceBus.Testing/issues/122

于 2017-11-13T13:13:36.060 回答