In previous versions of MassTransit, when writing unit tests, I used to seed initial state of a saga by calling the InMemorySagaRepository.Add() method:
private InMemorySagaRepository<TState> Repository { get; }
protected async Task SeedSaga<TState>(TState seedState)
where TState : class, SagaStateMachineInstance
{
await Repository.Add(new SagaInstance<TState>(seedState), default);
}
This method would add an entry for the saga in the desired state in the repository, which could be used as a starting point for my tests.
Now I upgraded to the version 7.2.1 of MassTransit and this method is no longer available.
Is it possible to seed data in the current version of the InMemorySagaRepository? If not, what approach could be taken to achive similar results?
Thanks.
There was a discussion on this late last year. In summary, you need to be using the container-based in-memory test harness.
var services = new ServiceCollection();
services.AddMassTransitInMemoryTestHarness(x =>
{
x.AddSagaStateMachineTestHarness<MySagaStateMachine, MySagaState>();
x.AddSagaStateMachine<MySagaStateMachine, MySagaState>()
.InMemoryRepository();
});
var provider = services.BuildServiceProvider();
Get and start the test harness (be sure to stop it in finally):
var harness = provider.GetRequiredService<InMemoryTestHarness>();
await harness.Start();
You can obtain the saga dictionary and add your seeded saga instance:
var dictionary = provider.GetRequiredService<IndexedSagaDictionary<MySagaState>>();
dictionary.Add(new SagaInstance(new MySagaState
{
CorrelationId = ...,
OtherProperty = ...
}));
The linked discussion has some useful extension methods as well.