[英]Unit test with multiple test cases, for method which returns different object types
I have static method: 我有静态方法:
public static ReturnBaseClass GetValue(SimpleBaseClass simpleBaseClass)
{
ReturnBaseClass returnBaseClass = null;
if (simpleBaseClass is simpleInheritedClass1)
{
returnBaseClass = new ReturnInheritedClass1();
}
else if (simpleBaseClass is simpleInheritedClass2)
{
returnBaseClass = new ReturnInheritedClass2();
}
else if (simpleBaseClass is simpleInheritedClass3)
{
returnBaseClass = new ReturnInheritedClass3();
}
return returnBaseClass;
}
According to type of object in method parameter (simpleBaseClass), method should return another type of object. 根据方法参数(simpleBaseClass)中对象的类型,方法应返回另一种对象。 This 'if else' branch is quite long and I wanna write simple unit test to test this.
这个'if else'分支很长,我想编写简单的单元测试来对此进行测试。 I am stuck with checking return object type.
我坚持检查返回对象类型。 I wrote test method:
我写了测试方法:
[TestCase(new simpleInheritedClass1(), Type ReturnInheritedClass1)]
[TestCase(new simpleInheritedClass2(), )]
[TestCase(new simpleInheritedClass3(), )]
public void GetValueTest(SimpleBaseClass simpleBaseClass, Type type)
{
}
What I did in first Test Case (Type ReturnInheritedClass1) does not work. 我在第一个测试用例(类型ReturnInheritedClass1)中所做的工作不起作用。 I do not know how to forward type of object to my Unit test in this way ?
我不知道如何以这种方式将对象类型转发到我的单元测试?
Try to use TestCaseSource attribute: 尝试使用TestCaseSource属性:
[TestCaseSource("Source")]
public void GetValueTest(SimpleBaseClass simpleBaseClass, ReturnInheritedClassBase simpleInheritedClass)
{
}
private static readonly object[] Source = new object[]
{
new object[] { new SimpleInheritedClass1(), new ReturnInheritedClass1()},
new object[] { new SimpleInheritedClass2(), new ReturnInheritedClass2()}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.