我有一个 switch 语句在一个工厂里,根据传入的枚举值返回一个命令。类似这样:
public ICommand Create(EnumType enumType)
{
switch (enumType)
{
case(enumType.Val1):
return new SomeCommand();
case(enumType.Val2):
return new SomeCommand();
case(enumType.Val3):
return new SomeCommand();
default:
throw new ArgumentOutOfRangeException("Unknown enumType" + enumType);
}
}
目前我为枚举中的每个值编写了一个switch case。我为每个case编写了单元测试。如何对默认情况进行单元测试以抛出错误?显然,此时我无法传入未知的EnumType,但谁能保证这不会在将来发生变化。是否有任何方式可以仅为了单元测试而扩展或模拟EnumType?