vendredi 15 juin 2018

How do i wireup NServiceBus' SqlSaga in a test scenario?

https://docs.particular.net/nservicebus/testing/ has a very neat example of how to test a simple Saga. Unfortunately it does not explain, how to do the same with a SqlSaga - ie. a Saga with persistence of state to a database.

Given this saga:

public class SomeFancySaga : SqlSaga<MySagaData>,
    IAmStartedByMessages<ImportantCommand>
{
    protected override string CorrelationPropertyName => nameof(Data.MyPrimaryKey);

    protected override void ConfigureMapping(IMessagePropertyMapper mapper)
    {
        mapper.ConfigureMapping<ImportantCommand>(x => x.CommandId);
    }

    public async Task Handle(ImportantCommand command, IMessageHandlerContext context)
    {
        if (Data.State == MyState.ReadyForUse)
            <do some stuff>
    }
    ...
}

If I try to write test-code like the example in the link, I would do something like this:

// arrange
var context = new NServiceBus.Testing.TestableMessageHandlerContext();
var command = ImportantCommand.Create();
var sut = new CreateSomeFancySaga();

// act
sut.Handle(command, context);

// assert
...

The call to sut.Handle() will throw a NullReferenceException, because the Saga property Data has not been initialized.

How do I correctly wireup the saga for testing so that:

  1. Data is initialized
  2. A real database connection is not really needed

Aucun commentaire:

Enregistrer un commentaire