我正在尝试为我的状态机创建单元测试,以检查每个状态是否都做了它应该做的事情。
我可以通过发布启动状态机的事件并创建达到所需状态所需的所有变量来使状态机进入任何状态。
简化状态机:
public class CheckFilesStateMachine : MassTransitStateMachine<DataState>, ICheckFilesStateMachine
{
#region Events
public Event<IEventCheckFile> CheckFile { get; private set; }
#endregion
#region States
public State CheckingForFiles { get; }
public State ValidatingFiles { get; }
public State NoFilesFound { get; }
public State Validated { get; }
public State FilesProcessed { get; }
public State ValidationFailed{ get; }
#endregion
public DataStateMachine(IDataService dataService)
{
InstanceState(x => x.CurrentState);
Event(() => CheckFile, x => x.CorrelateById(context => context.Message.Id));
Initially(When(CheckFile).TransitionTo(CheckingForFiles));
WhenEnter(CheckingForFiles, binder => binder
.Then(x => x.Instance.Files= dataService.GetFiles())
.IfElse(x => dataService.HasFiles(),
x => x.TransitionTo(ValidatingFiles),
x => x.TransitionTo(NoFilesFound))
.Catch<Exception>(ex => ex
.Then(x => Console.WriteLine(x.Exception.Message))));
WhenEnter(ValidatingFiles, binder => binder
.IfElse(x => dataService.ValidateFiles(),
x => x.TransitionTo(Validated),
x => x.TransitionTo(ValidationFailed))
.Catch<Exception>(ex => ex
.Then(x => Console.WriteLine(x.Exception.Message))));
WhenEnter(NoFilesFound, binder => binder.Then(x => Console.WriteLine("No Files found")).Finalize());
WhenEnter(Validated, binder => binder.Then(x => Console.WriteLine("Validated")).Finalize());
WhenEnter(ValidationFailed, binder => binder.Then(x => Console.WriteLine("Validation Failed")).Finalize());
SetCompletedWhenFinalized();
}
}
我写了一些单元测试,我可以启动机器(按照MassTransit 文档中的本指南),但我想检查状态CheckingForFiles和ValidatingFiles是否正在做它们应该独立做的事情。
到目前为止,我可以通过跟随状态机的流程来模拟IDataService方法以达到所需的状态,但是有没有办法启动状态机并跳转到特定状态?
更新
假设我在那里有另一个状态:
public class CheckFilesStateMachine : MassTransitStateMachine<DataState>, ICheckFilesStateMachine
{
#region Events
public Event<IEventCheckFile> CheckFile { get; private set; }
#endregion
#region States
public State CheckingForFiles { get; }
public State ValidatingFiles { get; }
public State DeletingFiles{ get; }
public State NoFilesFound { get; }
public State Validated { get; }
public State FilesProcessed { get; }
public State ValidationFailed{ get; }
#endregion
public DataStateMachine(IDataService dataService)
{
InstanceState(x => x.CurrentState);
Event(() => CheckFile, x => x.CorrelateById(context => context.Message.Id));
Initially(When(CheckFile).TransitionTo(CheckingForFiles));
WhenEnter(CheckingForFiles, binder => binder
.Then(x => x.Instance.Files= dataService.GetFiles())
.IfElse(x => dataService.HasFiles(),
x => x.TransitionTo(ValidatingFiles),
x => x.TransitionTo(NoFilesFound))
.Catch<Exception>(ex => ex
.Then(x => Console.WriteLine(x.Exception.Message))));
WhenEnter(ValidatingFiles, binder => binder
.IfElse(x => dataService.ValidateFiles(),
x => x.TransitionTo(Validated),
x => x.TransitionTo(DeletingFiles))
.Catch<Exception>(ex => ex
.Then(x => Console.WriteLine(x.Exception.Message))));
WhenEnter(DeletingFiles, binder => binder
.Then(x => x.Instance.Files= dataService.RemoveFiles())
.TransitionTo(ValidationFailed)
.Catch<Exception>(ex => ex
.Then(x => Console.WriteLine(x.Exception.Message))));
WhenEnter(NoFilesFound, binder => binder.Then(x => Console.WriteLine("No Files found")).Finalize());
WhenEnter(Validated, binder => binder.Then(x => Console.WriteLine("Validated")).Finalize());
WhenEnter(ValidationFailed, binder => binder.Then(x => Console.WriteLine("Validation Failed")).Finalize());
SetCompletedWhenFinalized();
}
}
在这种情况下,我可以输入CheckingForFiles然后跳转到DeletingFiles吗?可能吗?