我正在尝试Activity
对我的 MassTransit 状态机传奇的自定义进行单元测试。
它看起来像这样:
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);
}
我想不通的是,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)
{