How to create unit tests for NServiceBus Saga?
Asked Answered
B

3

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).
Blanka answered 10/2, 2011 at 5:37 Comment(0)
W
8

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

    }
}
Weyermann answered 21/2, 2011 at 14:13 Comment(0)
L
7

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

Lesbos answered 10/2, 2011 at 6:49 Comment(0)
T
0

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);
}
Tool answered 4/1, 2019 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.