简体   繁体   English

如何测试我的ConfigureHowToFindSaga是否在NServiceBus中正常工作?

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

I realized today, the hard way, that the saga testing doesn't uses the ConfigureHowToFindSaga . 今天,我艰难地意识到,传奇测试没有使用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. 由于我的测试未涵盖映射,因此导致生产中出现“未找到Saga”异常,我认为应该如此。 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. 当我添加ConfigureHowToFindSaga方法时,第二个测试应该通过。 For some reason when testing sagas this is not considered. 由于某些原因,在测试Sagas时不考虑这一点。 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? 应该如何测试这种情况?

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://github.com/Particular/NServiceBus/issues/654

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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