Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

859
Views
How to seed initial state to a saga in MassTransit test harness?

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!