Search code examples
nservicebus

Nsb: Unit Testing Saga Handlers which call ReplyToOriginator


I'm writing simple Unit Tests for all my Saga Handlers, basically just making sure the Messages are being caught/handled at all.

However, one of the Saga Handlers calls ReplyToOriginator and right now it's throwing an Exception because the Test Context isn't recognized as a valid Originator.

More specifically, my relevant code looks like this:

    [TestInitialize]
    public void Initialize()
    {
        _context = new TestableMessageHandlerContext();
        _process = new ActionProcess() { Data = new ActionProcess.ActionSagaData() };
    }

    [TestMethod]
    public async Task ActionProcess_ActionSentHandler_ShouldHandleMessage()
    {
        // Arrange
        var cmd = new ActionSent();

        // Act
        await _process.Handle(cmd, _context);

        // Assert
    }


    public async Task Handle(ActionSent message, IMessageHandlerContext context)
    {
        var actionProcessCompletedReply = new ActionProcessCompleted() 
        await ReplyToOriginator(context, actionProcessCompletedReply);
    }

And the Test fails with the following Exception being passed up:

System.Exception: Entity.Originator cannot be null. Perhaps the sender is a SendOnly endpoint.

Can anyone offer some advice as to how to solve this problem? Am I perhaps testing the Saga Handlers in a way that's not appropriate?


Solution

  • Saga.ReplyToOriginator expects to have originator set from the original message coming in. Since this is a test, the originator is not set automatically and you'd have to set it manually by assigning _process.Data.Originator some arbitrary value.