How do I test that my ConfigureHowToFindSaga is working in NServiceBus?

a 夏天 提交于 2019-12-11 03:43:03

问题


I realized today, the hard way, that the saga testing doesn't uses the ConfigureHowToFindSaga. This results in "Saga not found" exception showing up in production since my test doesn't cover the mapping, which I think it should. I have created a really simple example saga illustrating the problem:

public class MySpecialSaga : Saga<MySagaData>,
    IAmStartedByMessages<StartMessage>,
    IHandleMessages<NextMessage>
{
    public void Handle(StartMessage message)
    {
        Data.SagaId = message.Id;
        Console.WriteLine("Saga started with id: " + message.Id);
    }

    public void Handle(NextMessage message)
    {
        Console.WriteLine("Handling next message with id: " + message.Id);
        Bus.Send(new StartMessage() {Id = message.Id + 1});
    }
}

public class NextMessage : ICommand
{
    public int Id { get; set; }
}

public class StartMessage : ICommand
{
    public int Id { get; set; }
}

public class MySagaData : IContainSagaData
{
    public Guid Id { get; set; }
    public string Originator { get; set; }
    public string OriginalMessageId { get; set; }
    public int SagaId { get; set; }
}

Now I have the following two tests:

[TestFixture]
public class MySpecialSagaTests
{
    public MySpecialSagaTests()
    {
        Test.Initialize();
    }

    [Test]
    public void WhenSagaDoesNotExistItShouldNotFindSaga()
    {
        Test.Saga<MySpecialSaga>()
            .ExpectNotSend<StartMessage>(m => m.Id == 2)
            .When(s => s.Handle(new NextMessage() {Id = 1}));
    }

    [Test]
    public void WhenSagaDoesExistItShouldFindSaga()
    {
        Test.Saga<MySpecialSaga>()
            .When(s => s.Handle(new StartMessage(){ Id = 1}))
            .ExpectNotSend<StartMessage>(m => m.Id == 2)
            .When(s => s.Handle(new NextMessage() {Id = 1}));
    }
}

The first one should fail, or at least not send the message, since it shouldn't be able to find the saga. The second test should pass when I've added the ConfigureHowToFindSaga method. For some reason when testing sagas this is not considered. This can result in one having handlers in a saga that will not be executed due to a missing mapping.

How should this scenario be tested?


回答1:


There is no support for unit testing mappings as of now. The strict mode mentioned here will support this type of testing.

https://github.com/Particular/NServiceBus/issues/654



来源:https://stackoverflow.com/questions/19681055/how-do-i-test-that-my-configurehowtofindsaga-is-working-in-nservicebus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!