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

时间:2011-02-10 05:37:24

标签: c# unit-testing nservicebus

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

请参阅以下代码,不确定为何总是抱怨 谁知道我该怎么办呢?

(我正在使用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).

3 个答案:

答案 0 :(得分:8)

正如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));

    }
}

答案 1 :(得分:7)

为了测试一个传奇,你需要调用Test.Saga而不是Test.Handler。

答案 2 :(得分:0)

这里有一个带有一些示例的页面=> https://docs.particular.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);
}