NServicebus Test.Handler ExpectSend没有给出预期的结果

时间:2011-11-08 23:38:11

标签: nservicebus

前段时间我在yahoogroup上问了这个问题,但遗憾的是没有回答。

当将ExpectSend添加到unittest时,测试将失败并显示以下消息:

Rhino.Mocks.Exceptions.ExpectationViolationException: IBus.Send(callback method:
<>c__DisplayClass2`1.<ExpectSend>b__1); Expected #1, Actual #0

这看起来永远不会调用Bus.Send方法。但它是。 当没有使用.ExpectSend的行时,测试成功。

    [TestMethod()]
    public void Should_Handle_Goedkeuring_Which_Results_In_VolledigGoedgekeurdeFactuur()
    {
        int inkoopFactuurId = 5;

        Test.Initialize();

        var mock = new Mock<IProcesGoedkeuringDataService>();
        mock.Setup(x => x.IsFactuurVolledigGoedgekeurd(inkoopFactuurId)).Returns(true);

        Test.Handler<GoedkeuringDoorGebruikerCommandHandler>()
        .WithExternalDependencies(h => h.DataService = mock.Object)
        .ExpectSend<FactuurVolledigGoedgekeurdCommand>(c =>
        c.InkoopFactuurId == inkoopFactuurId)
        .OnMessage<GoedkeuringDoorGebruikerCommand>(m =>
        SetupMessage(m)); ;
    }

处理程序:在这种情况下,IsFactuurVolledigGoedgekeurd方法返回true。

public class GoedkeuringDoorGebruikerCommandHandler : IHandleMessages<GoedkeuringDoorGebruikerCommand>
{
    public IBus Bus { get; set; }
    public IProcesGoedkeuringDataService DataService { get; set; }

    public void Handle(GoedkeuringDoorGebruikerCommand message)
    {
        RegistreerGoedkeuring(message.InkoopFactuurId, message.GebruikerId);

        bool volledigGoedgekeurd = IsFactuurVolledigGoedgekeurd(message.InkoopFactuurId);
        if (volledigGoedgekeurd)
        {
            Bus.Send(new FactuurVolledigGoedgekeurdCommand(message.InkoopFactuurId));
        }
    }
}

1 个答案:

答案 0 :(得分:1)

检查此代码(基于RequestResponse NServiceBus示例):

[TestFixture]
public class Tests
{
    [Test]
    public void TestHandler()
    {
        var assemblies = new[]
                         {
                             typeof(RequestDataMessageHandler).Assembly,
                             typeof(RequestDataMessage).Assembly
                         };

        Test.Initialize(assemblies);

        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        Test.Handler<RequestDataMessageHandler>()
            .WithExternalDependencies(m => m.Repository = (new Mock<IRepository>()).Object)
            .ExpectSend<RequestDataMessage>(m => m.DataId == dataId && m.String == str && m.SecretQuestion == secret)
            .OnMessage<RequestDataMessage>(m => { m.DataId = dataId; m.String = str; m.SecretQuestion = secret; });
    }

    [Test]
    public void TestHandler2()
    {
        var assemblies = new[]
                         {
                             typeof(RequestDataMessageHandler).Assembly,
                             typeof(RequestDataMessage).Assembly
                         };

        Test.Initialize(assemblies);

        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        Test.Handler<RequestDataMessageHandler>()
            .WithExternalDependencies(m => m.Repository = (new Mock<IRepository>()).Object)
            .ExpectSend<RequestDataMessage>(Check)
            .OnMessage<RequestDataMessage>(m => { m.DataId = dataId; m.String = str; m.SecretQuestion = secret; });
    }

    private static bool Check(RequestDataMessage m)
    {
        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        return m.DataId == dataId && m.String == str && m.SecretQuestion == secret;
    }

    [Test]
    public void TestHandler3()
    {
        var assemblies = new[]
                         {
                             typeof(RequestDataMessageHandler).Assembly,
                             typeof(RequestDataMessage).Assembly
                         };

        Test.Initialize(assemblies);

        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        Test.Handler<RequestDataMessageHandler>()
            .WithExternalDependencies(m => m.Repository = (new Mock<IRepository>()).Object)
            .ExpectSend<RequestDataMessage>(m => m.DataId == dataId && m.String == str && m.SecretQuestion == secret)
            .OnMessage<RequestDataMessage>(m => SetUp(m));
    }

    private static void SetUp(RequestDataMessage m)
    {
        var dataId = Guid.NewGuid();
        var str = "hello";
        WireEncryptedString secret = "secret";

        m.DataId = dataId;
        m.String = str;
        m.SecretQuestion = secret;
    }

输出: 通过 2.失败 3.失败

(Rhino.Mocks.Exceptions.ExpectationViolationException:IBus.Send(回调方法:&lt;&gt; c_ DisplayClass2`1.b _1);预期#1,实际#0。)

使用您的方法作为ExpectSend / OnMessage方法的操作可能会破坏RhinoMocks的期望。我不知道为什么,但这就是出现异常的原因。