我对C#
他们的单元测试设施不熟悉。state
我使用来自的模式编写了一个状态机GoF
public class Program
{
private static void Main()
{
var c = new Context(SimpleStateMachine.StateInit);
c.Run();
Console.Read();
}
}
public static class SimpleStateMachine
{
public static void StateInit(Context context)
{
// ... do some init
context.State = StateConfigure;
}
public static void StateConfigure(Context context)
{
// do some conifigure
context.State = StateMeasurement;
}
public static void StateMeasurement(Context context)
{
// do some measurement
context.State = GetMeasurement()? StateConfigure : StateFinished;
}
public static void StateFinished(Context context)
{
// do some deinit
context.State = (Action<Context>)null;
}
}
public class Context
{
public Action<Context> State { get; internal set; }
public Context(Action<Context> state)
{
State = state;
}
public void Run()
{
while (State != null)
{
State(this);
}
}
}
如何正确测试这样的状态机?这个具体的状态机只是一个示例实现。正确的实现有更多的状态和条件进行转换。