简体   繁体   中英

How to use MassTransit test harness to test sagas with Activities (Automatonymous.Binders.EventActivityBinder) with constructor dependency injection?

To keep saga code simple I created activities that are executed when a saga event is triggered. Now I'd like to create tests for the saga using MT's test harness. I also need to mock dependencies from these activities. How can this be done in MT 7.0.2?

This question is similar to the one found here: How to use MassTransit test harness to test Consumer with constructor dependency injection?

Below is some pseudo code to exemplify my use case:

public class SomeSaga : MassTransitStateMachine<SomeSagaState>
{
    public Event<InitializeCommand> Initialize { get; set; }
    
    public State Initialized { get; set; }

    public SomeSaga()
    {
        InstanceState(x => x.CurrentState);
        Event(() => Initialize, e => { e.CorrelateById(c => c.Message.CorrelationId); });

        Initially(
            When(Initialize)
                .Activity(x => x.OfType<InitializeActivity>())
                .TransitionTo(Initialized));
    }
}

public class InitializeActivity : Activity<SomeSagaState, InitializeCommand>
{
    private readonly ISomeDependency _dep;

    public InitializeActivity(ISomeDependency dep)
    {
        _dep = dep ?? throw new ArgumentNullException(nameof(dep));
    }
    
    public async Task Execute(...)
    {
        // do something
    }
}

You need at least v7.0.4 of MassTransit, though I'd suggest using the latest version. Container support for the InMemoryTestHarnesswas added in that version.

There are also extensive examples of how to use it in Sample-Library .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM