NserviceBus.Testing not using ISagaFinder for saga

I have a saga that I am trying to write scenario tests for, and one type of message requires a custom finder. The finder is being called when running the saga normally but if I send that type of message to the TestableSaga from NServiceBus.Testing there is a fault and no saga is found.

We are using NHibernate for our normal Saga persistence, but I am not sure if that affects the testing in any way.

var ackResult = testableSaga.Handle(new Z3Messages.SmartphonePush.AckMessage()
{
    RecipientID = "11"
});

In this instance ackResult will have the status of faulted and I can confirm that the ISagaFinder is not being called. Here is my saga finder.

public class PushSagaFinder : ISagaFinder<PushSagaData, AckMessage>
{
    public Task<PushSagaData> FindBy(AckMessage message, ISynchronizedStorageSession storageSession, IReadOnlyContextBag context, CancellationToken cancellationToken = default)
    {
        var session = storageSession.Session();
        var orderSagaData = session.QueryOver<PushSagaData>()
            .Where(d => d.LastRecipientID == message.RecipientID)
            .SingleOrDefault();
        return Task.FromResult(orderSagaData);
    }
}

Is there some additional setup that needs to be done to get the tests to use the saga finder?

The testable saga is designed for unit testing, not integration testing. It’s all in memory, which is why saga finders are not invoked.

You should be able to unit-test sagas and saga finders in isolation. What’s leading you to need to test them together?

I do have another question, what’s the business scenario that requires saga finders in your case?

I am not trying to test the saga finder in this scenario, I am trying to test the handler for the message type that is handled by the finder. So when I call this

 var ackResult = testableSaga.Handle(new Z3Messages.SmartphonePush.AckMessage()
 {
     RecipientID = "11"
 });

It returns a fault in the ackResult and doesn’t actually call the handler for AckMessage in the saga. The reason for the finder is because the AckMessage uses a different property of the saga data to match on than the rest of the messages the saga handles. If I try to setup the mapper that way it throws an exception on startup that there are multiple properties matched in the mapper.

I guess the real question should be, how do I get the testable saga to call the handler for a message type that uses an ISagaFinder?

@jupham,

Now I understand what the culprit is. I reproduced the problem and raised a spike PR with a potential solution: [Spike] When using saga finders the testable saga fails by mauroservienti · Pull Request #651 · Particular/NServiceBus.Testing · GitHub

At the moment, it’s a spike. I haven’t investigated the ramifications and I dislike the test fake finders requirements. It should provide a better API.