如何为 NServiceBus Saga 创建单元测试?

发布于 2024-10-16 11:42:12 字数 2586 浏览 0 评论 0原文

我正在尝试按照这篇文章(http://blog.zoolutions.se/post/2010/04/01/Conquering-NServiceBus-part-4-e28093-Testing.aspx)为我的 nservicebus saga 项目创建单元测试

请参阅下面的代码,不知道为什么它总是抱怨 有人知道我该如何修复它吗?

(我使用的是nservice总线2.0)

public class ReportSaga : Saga<ReportSagaData>,
                          IAmStartedByMessages<RequestReportMessage>,
                          IHandleMessages<PollingReportStatusMessage>
{
// implementation
}



[TestFixture]
    public class ReportSaga_HandleRequestReportMessageTests
    {
        [TestFixtureSetUp]
        public void SetUp()
        {
            var assemblies = new[]
                         {
                             typeof (ReportSaga).Assembly,
                             typeof (RequestReportMessage).Assembly,
                             typeof (PollingReportStatusMessage).Assembly,
                             Assembly.Load("NServiceBus"),
                             Assembly.Load("NServiceBus.Core")
                         };

            Test.Initialize(assemblies);
        }

        [Test]
        public void HandleRequestReportMessageTests()
        {

            Test.Handler<ReportSaga>()
                .OnMessage<RequestReportMessage>(x =>
                {
                    x.Id = 1234;
                    x.ReportDate = DateTime.Now;
                });


        }
    }


Test 'UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests' failed: System.ArgumentException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type 'T'.
  ----> System.TypeLoadException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type parameter 'T'.
    at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
    at System.RuntimeType.MakeGenericType(Type[] instantiation)
    at NServiceBus.Testing.Test.Handler[T](T handler)
    at NServiceBus.Testing.Test.Handler[T]()
    ReportSaga_HandleRequestReportMessageTests.cs(34,0): at UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests()
    --TypeLoadException
    at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
    at System.RuntimeTypeHandle.Instantiate(Type[] inst)
    at System.RuntimeType.MakeGenericType(Type[] instantiation)

0 passed, 1 failed, 0 skipped, took 1.11 seconds (NUnit 2.5.5).

I am trying to follow this article (http://blog.zoolutions.se/post/2010/04/01/Conquering-NServiceBus-part-4-e28093-Testing.aspx) to create unit test for my nservicebus saga project

See the following code, not sure why it always complain
anyone know how can i fix it?

(I am using nservice bus 2.0)

public class ReportSaga : Saga<ReportSagaData>,
                          IAmStartedByMessages<RequestReportMessage>,
                          IHandleMessages<PollingReportStatusMessage>
{
// implementation
}



[TestFixture]
    public class ReportSaga_HandleRequestReportMessageTests
    {
        [TestFixtureSetUp]
        public void SetUp()
        {
            var assemblies = new[]
                         {
                             typeof (ReportSaga).Assembly,
                             typeof (RequestReportMessage).Assembly,
                             typeof (PollingReportStatusMessage).Assembly,
                             Assembly.Load("NServiceBus"),
                             Assembly.Load("NServiceBus.Core")
                         };

            Test.Initialize(assemblies);
        }

        [Test]
        public void HandleRequestReportMessageTests()
        {

            Test.Handler<ReportSaga>()
                .OnMessage<RequestReportMessage>(x =>
                {
                    x.Id = 1234;
                    x.ReportDate = DateTime.Now;
                });


        }
    }


Test 'UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests' failed: System.ArgumentException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type 'T'.
  ----> System.TypeLoadException : GenericArguments[0], 'ReportSagaData', on 'NServiceBus.IMessageHandler`1[T]' violates the constraint of type parameter 'T'.
    at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Exception e)
    at System.RuntimeType.MakeGenericType(Type[] instantiation)
    at NServiceBus.Testing.Test.Handler[T](T handler)
    at NServiceBus.Testing.Test.Handler[T]()
    ReportSaga_HandleRequestReportMessageTests.cs(34,0): at UnitTests.ReportSaga_HandleRequestReportMessageTests.HandleRequestReportMessageTests()
    --TypeLoadException
    at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle handle, IntPtr* pInst, Int32 numGenericArgs, ObjectHandleOnStack type)
    at System.RuntimeTypeHandle.Instantiate(Type[] inst)
    at System.RuntimeType.MakeGenericType(Type[] instantiation)

0 passed, 1 failed, 0 skipped, took 1.11 seconds (NUnit 2.5.5).

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

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

发布评论

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

评论(3

时光磨忆 2024-10-23 11:42:12

正如 Udi 所说,但是语法应该如下所示:

[TestFixture]
public class ReportSaga_HandleRequestReportMessageTests
{
    [TestFixtureSetUp]
    public void SetUp()
    {
        var assemblies = new[]
                     {
                         typeof (ReportSaga).Assembly,
                         typeof (RequestReportMessage).Assembly,
                         typeof (PollingReportStatusMessage).Assembly,
                         Assembly.Load("NServiceBus"),
                         Assembly.Load("NServiceBus.Core")
                     };

        Test.Initialize(assemblies);
    }

    [Test]
    public void HandleRequestReportMessageTests()
    {

        var message = new RequestReportMessage { Id = 1234, ReportDate = DateTime.Now };

        Test.Saga<ReportSaga>()
            .ExpectPublish<PublishMessage>(e => e.SomePropertyOfPublishMethod == "value")
            .When(x => x.Handle(message));

    }
}

Exactly as Udi said, However the syntax should look something like this:

[TestFixture]
public class ReportSaga_HandleRequestReportMessageTests
{
    [TestFixtureSetUp]
    public void SetUp()
    {
        var assemblies = new[]
                     {
                         typeof (ReportSaga).Assembly,
                         typeof (RequestReportMessage).Assembly,
                         typeof (PollingReportStatusMessage).Assembly,
                         Assembly.Load("NServiceBus"),
                         Assembly.Load("NServiceBus.Core")
                     };

        Test.Initialize(assemblies);
    }

    [Test]
    public void HandleRequestReportMessageTests()
    {

        var message = new RequestReportMessage { Id = 1234, ReportDate = DateTime.Now };

        Test.Saga<ReportSaga>()
            .ExpectPublish<PublishMessage>(e => e.SomePropertyOfPublishMethod == "value")
            .When(x => x.Handle(message));

    }
}
焚却相思 2024-10-23 11:42:12

为了测试 saga,您需要调用 Test.Saga 而不是 Test.Handler。

In order to test a saga, you need to call Test.Saga rather than Test.Handler.

定格我的天空 2024-10-23 11:42:12

这里有一个包含一些示例的页面 => https://docs.pspecial.net/samples/unit-testing/

关注上面页面中的 1 个示例,稍加修改:

[Test]
public async Task ShouldProcessDiscountOrder()
{
    // Arrange
    var saga = new YourSaga
    {
        Data = new YourSagaData()
    };

    var context = new TestableMessageHandlerContext();

    var yourCommand = new MyCommandOrEvent
    {
        propA = 1
    };

    // Act
    await saga.Handle(yourCommand, context)
        .ConfigureAwait(false);

    // Assert
    var processMessage = (OutputTypeReturnedByHandle)context.SentMessages[0].Message;
    Assert.AreEqual(123, processMessage.Something);
}

There is a page with some samples here => https://docs.particular.net/samples/unit-testing/

Follows 1 example from the page above, with minor modifications:

[Test]
public async Task ShouldProcessDiscountOrder()
{
    // Arrange
    var saga = new YourSaga
    {
        Data = new YourSagaData()
    };

    var context = new TestableMessageHandlerContext();

    var yourCommand = new MyCommandOrEvent
    {
        propA = 1
    };

    // Act
    await saga.Handle(yourCommand, context)
        .ConfigureAwait(false);

    // Assert
    var processMessage = (OutputTypeReturnedByHandle)context.SentMessages[0].Message;
    Assert.AreEqual(123, processMessage.Something);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文