如何使用 NServiceBus 验收测试从 saga 处理程序获得反馈
How to get feedback from saga handler using NServiceBus Acceptance Testing
背景
我已经编写了一个测试来确保应该启动我的传奇的命令有效地创建了传奇并且它的处理程序代码可以被执行:
[Fact]
public async Task Can_Start_Saga_And_Execute_Handler()
{
var result = await Scenario
.Define<Context>()
.WithEndpoint<Endpoint>(b => b.When(session =>
{
return session.SendLocal(new SagaStarter());
})
)
.Done(context => context.IsRequested)
.Run(Testing.MaxRunTime);
result.IsRequested.ShouldBeTrue();
}
上下文是:
class Context : ScenarioContext
{
public bool IsRequested { get; set; }
}
所以
如果我的 saga 定义中有这样一个处理程序:
public async Task Handle(SagaStarter message, IMessageHandlerContext context)
{
await StuffToDo();
}
How can I ensure that the IsRequested
property, defined in
Context
, is set to true from within the saga?
我找到了一个解决方案,在测试中添加了一个额外的消息处理程序,如下所示:
public class TestHandler : IHandleMessages<SagaStarter>
{
private Context _testContext;
public TestHandler(Context testContext)
{
_testContext = testContext;
}
public Task Handle(SagaStarter message, IMessageHandlerContext context)
{
_testContext.IsRequested= true;
return Task.CompletedTask;
}
}
Context
参数是通过Dependency Injection插入的。
背景
我已经编写了一个测试来确保应该启动我的传奇的命令有效地创建了传奇并且它的处理程序代码可以被执行:
[Fact]
public async Task Can_Start_Saga_And_Execute_Handler()
{
var result = await Scenario
.Define<Context>()
.WithEndpoint<Endpoint>(b => b.When(session =>
{
return session.SendLocal(new SagaStarter());
})
)
.Done(context => context.IsRequested)
.Run(Testing.MaxRunTime);
result.IsRequested.ShouldBeTrue();
}
上下文是:
class Context : ScenarioContext
{
public bool IsRequested { get; set; }
}
所以
如果我的 saga 定义中有这样一个处理程序:
public async Task Handle(SagaStarter message, IMessageHandlerContext context)
{
await StuffToDo();
}
How can I ensure that the
IsRequested
property, defined inContext
, is set to true from within the saga?
我找到了一个解决方案,在测试中添加了一个额外的消息处理程序,如下所示:
public class TestHandler : IHandleMessages<SagaStarter>
{
private Context _testContext;
public TestHandler(Context testContext)
{
_testContext = testContext;
}
public Task Handle(SagaStarter message, IMessageHandlerContext context)
{
_testContext.IsRequested= true;
return Task.CompletedTask;
}
}
Context
参数是通过Dependency Injection插入的。