How to create unit tests for NServiceBus Saga?

testablemessagehandlercontext
nservicebus testableendpointinstance
nservicebus endpointconfiguration
nservicebus documentation
nservicebus multiple endpoints
c# nservicebus

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

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

    }
}

Testing NServiceBus • Testing • Particular Docs, Develop service layers and long-running processes using test-driven Testing , is provided with tools for unit testing endpoint handlers and sagas. TimeoutMessages, A list of all messages resulting from use of Saga. This sample shows how to write unit tests for various NServiceBus components with Arrange-Act-Assert (AAA) style tests. This sample is a test project that uses NUnit and testable helper implementations from the NServiceBus. Testing package. Testing a handler. Given the following handler:

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

Testing NServiceBus with fluent style • Testing • Particular Docs, Develop service layers and long-running processes using test-driven Sagas should also be tested with a focus on their externally visible  Testing, is provided with tools for unit testing endpoint handlers and sagas. The testing package can be used with any .NET unit testing framework, such as NUnit, xUnit.net or MSTest. Testing a handler. Testing a message handler is done using the TestableMessageHandlerContext class provided by the NServiceBus. Testing package.

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

How to create unit tests for NServiceBus Saga when Saga has more , For other readers reference, the proper test structure should be similar to: Test.​Saga<TestSagaHandler>(sagaId) .When(x => { x.Handle(new  To test this saga add a unittest project to your solution, add references to the saga and command projects, install the NServiceBus.Testing package. Below the test methods for the saga. Since I use the tips from Art of unit testing this should be readable for you.

Unit testing NServiceBus, This includes anything that a message handler and saga can do, including The very basics of starting unit testing is to create a unit test project in Visual Studio  Unit of Work Usage How to create a custom unit of work. Unit Testing NServiceBus Writing unit tests for NServiceBus systems. Unobtrusive Demonstrates NServiceBus operating in unobtrusive mode. Versioning Message evolution over time using interfaces. WCF to messaging bridge Reliable WCF communication over messaging. WebSocket Gateway

Learning NServiceBus Sagas, design 116 The source code 118 Adding NServiceBus to MVC 120 Message handler unit testing 123 Saga handler unit testing 128 Integration tests with MVC​  If you do a custom saga finder, it doesn't make sense to have anything in ConfigureHowToFindSaga. Yet, the definition of the Saga is no different. Something to keep in mind should we ever entertain the idea about asserting anything about what happens inside the ConfigureHowToFindSaga.

Exploring NServiceBus Integration Testing options, NServiceBus comes with a powerful unit testing framework. We could say that if users are testing their components (handlers, sagas, behaviors, etc…) EndpointConfiguration class directly I'm creating a class the inherits  The saga is a pattern that addresses the challenges uncovered by the relational database community years ago, packaged in NServiceBus for ease of use. One of the common mistakes developers make when designing distributed systems is based on the assumptions that time is constant.