当 Saga 有多个处理程序时,如何为 NServiceBus Saga 创建单元测试

发布于 2024-11-11 16:08:45 字数 1395 浏览 2 评论 0原文

我曾经做过这个测试,它通过得很好(一旦处理了所有三个消息,传奇就完成了。

        Test.Saga<TestSagaHandler>(sagaId)
            .When(x =>
            {
                x.Handle(new TestSagaStartMessageOne
                {
                    Id = sagaId
                });
                x.Handle(new TestSagaStartMessageTwo
                {
                    Id = sagaId
                });
                x.Handle(new TestSagaNonStartingMessage
                {
                    Id = sagaId
                });
            });
            .AssertSagaCompletionIs(true);

我现在想将 TestSagaNonStartingMessage 分解到它自己的处理程序中,并执行以下操作:

        Test.Saga<TestSagaHandler>(sagaId)
            .When(x =>
            {
                x.Handle(new TestSagaStartMessageOne
                {
                    Id = sagaId
                });
                x.Handle(new TestSagaStartMessageTwo
                {
                    Id = sagaId
                });
            });

        Test.Saga<TestSagaHandlerSingleMessage>(sagaId)
            .When(x =>
                x.Handle(new TestSagaNonStartingMessage
                {
                    Id = sagaId
                })
            )
        .AssertSagaCompletionIs(true);

但是,在处理 TestSagaNonStartingMessage 时 - saga 数据没有从以前的处理程序中持久化。

我是否存在持久性问题,或者测试构建得不好?

I used to have this test, which passes just fine (The saga is complete once all three messages have been handled.

        Test.Saga<TestSagaHandler>(sagaId)
            .When(x =>
            {
                x.Handle(new TestSagaStartMessageOne
                {
                    Id = sagaId
                });
                x.Handle(new TestSagaStartMessageTwo
                {
                    Id = sagaId
                });
                x.Handle(new TestSagaNonStartingMessage
                {
                    Id = sagaId
                });
            });
            .AssertSagaCompletionIs(true);

I now want to break out the TestSagaNonStartingMessage into its own handler, and did the following:

        Test.Saga<TestSagaHandler>(sagaId)
            .When(x =>
            {
                x.Handle(new TestSagaStartMessageOne
                {
                    Id = sagaId
                });
                x.Handle(new TestSagaStartMessageTwo
                {
                    Id = sagaId
                });
            });

        Test.Saga<TestSagaHandlerSingleMessage>(sagaId)
            .When(x =>
                x.Handle(new TestSagaNonStartingMessage
                {
                    Id = sagaId
                })
            )
        .AssertSagaCompletionIs(true);

However, when handling the TestSagaNonStartingMessage - the saga data is not persisted from the previous handlers.

Am I having persistence problems, or is the test constructed badly?

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

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

发布评论

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

评论(2

[旋木] 2024-11-18 16:08:45

测试构建不正确 - 请查看制造示例中的测试项目以了解应如何构建它。简短的答案是将第二个 .When(...) 链接在第一个之后。

The test isn't constructed correctly - please look at the test project in the manufacturing sample to see how it should be structured. The short answer is to chain the second .When(...) after the first.

花开柳相依 2024-11-18 16:08:45

对于其他读者参考,正确的测试结构应类似于:

    Test.Saga<TestSagaHandler>(sagaId)
        .When(x =>
        {
            x.Handle(new TestSagaStartMessageOne { Id = sagaId });
            x.Handle(new TestSagaStartMessageTwo { Id = sagaId });
        })
        .When(x =>
            x.Handle(new TestSagaNonStartingMessage { Id = sagaId })
        )
        .AssertSagaCompletionIs(true);

正如 Udi 所示,将“When”子句链接在一起。

另外,为了从测试中获得更多价值,请考虑引入异常,例如 ExpectSend<>ExpectPublish 等。

参考:http://docs.pspecial.net/nservicebus/testing/

For other readers reference, the proper test structure should be similar to:

    Test.Saga<TestSagaHandler>(sagaId)
        .When(x =>
        {
            x.Handle(new TestSagaStartMessageOne { Id = sagaId });
            x.Handle(new TestSagaStartMessageTwo { Id = sagaId });
        })
        .When(x =>
            x.Handle(new TestSagaNonStartingMessage { Id = sagaId })
        )
        .AssertSagaCompletionIs(true);

As Udi indicated, chain the "When" clauses together.

Also, in order to derive further value from the test, consider introducing exceptions, such as ExpectSend<>, ExpectPublish, etc.

Reference: http://docs.particular.net/nservicebus/testing/

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