本文介绍了如何测试我的 ConfigureHowToFindSaga 是否在 NServiceBus 中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天艰难地意识到,saga 测试不使用 ConfigureHowToFindSaga.这导致生产中出现Saga not found"异常,因为我的测试不包括映射,我认为应该这样做.我创建了一个非常简单的示例传奇来说明问题:

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}));
    }
}

第一个应该失败,或者至少不发送消息,因为它应该无法找到传奇.当我添加了 ConfigureHowToFindSaga 方法后,第二个测试应该会通过.出于某种原因,在测试传奇时不考虑这一点.这可能会导致 saga 中的处理程序由于缺少映射而无法执行.

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?

推荐答案

目前不支持单元测试映射.这里提到的严格模式将支持这种类型的测试.

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

这篇关于如何测试我的 ConfigureHowToFindSaga 是否在 NServiceBus 中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:24