NServicebus Test.Handler ExpectSend 未给出预期结果

发布于 2024-12-14 19:54:39 字数 1743 浏览 4 评论 0原文

我前段时间在 yahoogroup 上问过这个问题,但不幸的是没有答案。

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

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));
        }
    }
}

I asked this some time ago on the yahoogroup but unfortunately no answer.

When ExpectSend is added to the unittest, the test fails with this message:

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

.

This looks like the Bus.Send method is never called. But it is.
When the line with .ExpectSend is not used, the test succeeded.

    [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)); ;
    }

The handler: The IsFactuurVolledigGoedgekeurd method returns true in this case.

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));
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2024-12-21 19:54:39

检查此代码(基于 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;
    }

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

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

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

Check this code (based on RequestResponse NServiceBus sample):

[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;
    }

Output:
1. Pass
2. Fail
3. Fail

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

Probably the using of your method applied as an action to ExpectSend/OnMessage method it breaks the RhinoMocks expectation. I do not know why, but it is the reason why exception appeared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文