NServiceBus单元测试:当bus.Send()时,ExpectSend未满足

时间:2017-11-08 01:39:20

标签: c# unit-testing nunit moq nservicebus5

我正在使用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);
    }
}

1 个答案:

答案 0 :(得分:2)

要弄清楚这有点棘手,但问题是由于你使用了send:

ExpectSendToDestination

您要发送到特定目的地(我建议依赖路由)。

测试框架具有特定的“期望”选项,用于发送到特定目的地。如果您使用.ExpectSendToDestination<GeneralLedgerAuditCommand>((command, destination) => command.SagaReferenceId == "ab") 代替,那么您的测试应该可以正常运行:

ExpectSend

在NServiceBus.Testing v6(依赖于NServiceBus v6)中,这已得到修复,并且在发送到特定目的地时也会调用$Session = New-Object -ComObject "Microsoft.Update.Session" if($?) { $Searcher = $Session.CreateUpdateSearcher() if($?) { $historyCount = $Searcher.GetTotalHistoryCount() if($?) { $a = $Searcher.QueryHistory(0, $historyCount) | Select- Object @{Name="Title";Expression={'"'+$_.Title'"'}}| Format-Table -HideTableHeaders if($?) { $combined=ForEach-Object {$a} } } } }

编辑:我在 NServiceBus.Testing 中提出了有关此混淆行为的问题,请参阅https://github.com/Particular/NServiceBus.Testing/issues/122