测试 MassTransit 状态机传奇 activity
Test MassTransit state machine saga activity
我正在尝试对我的 MassTransit 状态机传奇的自定义 Activity
进行单元测试。
看起来像这样:
public class UpdateActivity : Activity<UpdateState>
{
private readonly ConsumeContext _consumeContext;
private readonly ILogger<UpdateActivity> _logger;
public UpdateActivity(
ConsumeContext consumeContext,
ILogger<UpdateActivity> logger
)
{
_consumeContext = consumeContext;
_logger = logger;
}
public void Probe(ProbeContext context) => context.CreateScope(nameof(UpdateActivity));
public void Accept(StateMachineVisitor visitor) => visitor.Visit(this);
public async Task Execute(BehaviorContext<UpdateState> context, Behavior<UpdateState> next)
{
await DoStuffAsync(context.Instance);
await next.Execute(context).ConfigureAwait(false);
}
public async Task Execute<T>(BehaviorContext<UpdateState, T> context, Behavior<UpdateState, T> next)
{
await DoStuffAsync(context.Instance);
await next.Execute(context).ConfigureAwait(false);
}
public Task Faulted<TException>(BehaviorExceptionContext<UpdateState, TException> context, Behavior<UpdateState> next) where TException : Exception
=> next.Faulted(context);
public Task Faulted<T, TException>(BehaviorExceptionContext<UpdateState, T, TException> context, Behavior<UpdateState, T> next) where TException : Exception
=> next.Faulted(context);
}
我想不通的是,在为此 class 编写单元测试时,我如何 mock/fake 期望 ConsumeContext
。我尝试使用 InMemoryTestHarness
查找内容,但找不到合适的内容。
编辑:
我不妨把这个也扔进去。如何在测试工具中模拟 context
或 运行?这样我也可以对这个 Activity 进行单元测试?
public class UpdateActivity : Activity<UpdateState, IDataUpdatedEvent>
{
private readonly ILogger<UpdateActivity> _logger;
public UpdateActivity(
ILogger<UpdateActivity > logger
)
{
_logger = logger;
}
public void Probe(ProbeContext context) => context.CreateScope(nameof(UpdateActivity));
public void Accept(StateMachineVisitor visitor) => visitor.Visit(this);
public async Task Execute(BehaviorContext<UpdateState, IDataUpdatedEvent> context, Behavior<UpdateState, IDataUpdatedEvent> next)
{
MassTransit test harnesses 允许测试状态机,以及使用依赖注入的活动。
考虑到这些工具的可用性,“使用模拟进行隔离测试”的想法是毫无意义的。
我正在尝试对我的 MassTransit 状态机传奇的自定义 Activity
进行单元测试。
看起来像这样:
public class UpdateActivity : Activity<UpdateState>
{
private readonly ConsumeContext _consumeContext;
private readonly ILogger<UpdateActivity> _logger;
public UpdateActivity(
ConsumeContext consumeContext,
ILogger<UpdateActivity> logger
)
{
_consumeContext = consumeContext;
_logger = logger;
}
public void Probe(ProbeContext context) => context.CreateScope(nameof(UpdateActivity));
public void Accept(StateMachineVisitor visitor) => visitor.Visit(this);
public async Task Execute(BehaviorContext<UpdateState> context, Behavior<UpdateState> next)
{
await DoStuffAsync(context.Instance);
await next.Execute(context).ConfigureAwait(false);
}
public async Task Execute<T>(BehaviorContext<UpdateState, T> context, Behavior<UpdateState, T> next)
{
await DoStuffAsync(context.Instance);
await next.Execute(context).ConfigureAwait(false);
}
public Task Faulted<TException>(BehaviorExceptionContext<UpdateState, TException> context, Behavior<UpdateState> next) where TException : Exception
=> next.Faulted(context);
public Task Faulted<T, TException>(BehaviorExceptionContext<UpdateState, T, TException> context, Behavior<UpdateState, T> next) where TException : Exception
=> next.Faulted(context);
}
我想不通的是,在为此 class 编写单元测试时,我如何 mock/fake 期望 ConsumeContext
。我尝试使用 InMemoryTestHarness
查找内容,但找不到合适的内容。
编辑:
我不妨把这个也扔进去。如何在测试工具中模拟 context
或 运行?这样我也可以对这个 Activity 进行单元测试?
public class UpdateActivity : Activity<UpdateState, IDataUpdatedEvent>
{
private readonly ILogger<UpdateActivity> _logger;
public UpdateActivity(
ILogger<UpdateActivity > logger
)
{
_logger = logger;
}
public void Probe(ProbeContext context) => context.CreateScope(nameof(UpdateActivity));
public void Accept(StateMachineVisitor visitor) => visitor.Visit(this);
public async Task Execute(BehaviorContext<UpdateState, IDataUpdatedEvent> context, Behavior<UpdateState, IDataUpdatedEvent> next)
{
MassTransit test harnesses 允许测试状态机,以及使用依赖注入的活动。
考虑到这些工具的可用性,“使用模拟进行隔离测试”的想法是毫无意义的。