masstransitautomatonymous

Testing a MassTransit saga with scheduled events


Trying to follow the example on how to test a Saga that uses DI (https://masstransit-project.com/usage/testing.html#testing-using-dependency-injection)

 var provider = new ServiceCollection()
    .AddMassTransitInMemoryTestHarness(cfg =>
    {
        cfg.AddSagaStateMachine<TStateMachine, TInstance>()
            .InMemoryRepository();
        cfg.AddSagaStateMachineTestHarness<TStateMachine, TInstance>();
    })
    .BuildServiceProvider(true);

...

However, I'm not able to get tests working with Scheduled events on the Saga. I'm following the example at https://masstransit-project.com/usage/sagas/automatonymous.html#schedule

I've got it working with RabbitMQ by adding AddRabbitMqMessageScheduler and UseDelayedExchangeMessageScheduler in my messagebus configuration.

In the tests I get the following exception:

The payload was not found: MassTransit.MessageSchedulerContext

I guess it's because I haven't configured the provider with the right things. I'm trying to find the correct extension methods but I guess there might not be support for that in combination with AddMassTransitInMemoryTestHarness?


Solution

  • There is support, you just have to configure it:

    TestHarness = provider.GetRequiredService<InMemoryTestHarness>();
    TestHarness.OnConfigureInMemoryBus += configurator =>
    {
        configurator.UseDelayedMessageScheduler();
    };
    

    This must execute before starting the harness.