NUnit的3.0 TestCase的const的自定义对象参数 [英] NUnit 3.0 TestCase Const Custom Object Arguments
问题描述
我写的类 SomeObject
,我想静态定义此对象的实例,以保持/重用的单元测试。我应该如何重写下面的代码来实现这种行为?
[的TestFixture]
公共类SomeObjectTests {
私人常量SomeObject item0 =新SomeObject(0.0); //不起作用
[测试用例(item0,ExpectedResult = 0.0)]
公共双TestSomeObjectValue(SomeObject左右){
返回so.Value;
}
[测试用例(item0,ExpectedResult =0.0)]
公共字符串TestSomeObjectValueString(SomeObject左右){
返回so.ValueString;
}
}
我收到以下错误信息:
字符串以外的引用类型的常量字段只能用null初始化。
块引用>
解决方案的 C#语言规范,10.3节说(重点煤矿):
当为恒定值的符号名称是理想的,但是当值的类型在一个常数声明是不允许的,或当值不能在编译时通过一个常数表达式计算,
只读
字段(第10.4.2节)可以用来代替
块引用>
烦人,这是通过以下事实属性有一定的限制配合太 - 看到 C#语言规范,§17.2(再次,重点煤矿):
这是表达式E是一个属性参数表达式如果所有下面的语句是正确的:
E型是属性参数类型(17.1.3)
在编译时,E的值可以解析为下列之一:
一个恒定值。
一个系统。 Type对象。
属性参数表达式的一维数组。
块引用>
其中的§17.1.3:属性参数类型的说: 1 :
该类型属性类的定位和命名参数仅限于属性参数类型,它们是:
- 一个以下几类:
布尔
,字节
,字符
,双击
,浮动
,INT
,长
,短
,字符串
。
- 类型
对象
。
- 类型
的System.Type
。
- 枚举类型,只要它有公共可访问性和它嵌套的类型(如果有的话)也有公共可访问性( §17.2)。
- 以上类型的一维数组。
块引用>
1 :引用的文字是从旧版本的C#规范 - 中的 C#5.0版本,另外四个类型中提到:
为sbyte
,UINT
,ULONG
和USHORT
。
在换句话说,你能做的最好的是一样的东西:
[的TestFixture]
公共类SomeObjectTests {
私人静态只读SomeObject item0 =新SomeObject(0.0);
私有静态SomeObject的getObject(字符串键){
如果(键==item0)
返回item0;
抛出新的ArgumentException(未知键);
}
[测试用例(item0,ExpectedResult = 0.0)]
公共双TestSomeObjectValue(字符串键){
SomeObject所以=的getObject(键);
返回so.Value;
}
[测试用例(item0,ExpectedResult =0.0)]
公共字符串TestSomeObjectValueString(字符串键){
SomeObject所以=的getObject(键) ;
返回so.ValueString;
}
}
这样的话,参数的属性编制─时间常数,而
的getObject
方法可以处理获得SomeObject
实例。I've written the class
SomeObject
and I want to statically define an instance of this object to keep/reuse for a unittest. How should I rewrite the code below to achieve this behavior?[TestFixture] public class SomeObjectTests { private const SomeObject item0 = new SomeObject(0.0); // doesn't work [TestCase(item0, ExpectedResult = 0.0)] public double TestSomeObjectValue(SomeObject so) { return so.Value; } [TestCase(item0, ExpectedResult = "0.0")] public string TestSomeObjectValueString(SomeObject so) { return so.ValueString; } }
I get the following error message:
A const field of a reference type other than string can only be initialized with null.
解决方案The C# language spec, §10.3 says (emphasis mine):
When a symbolic name for a constant value is desired, but when the type of that value is not permitted in a constant declaration, or when the value cannot be computed at compile-time by a constant-expression, a
readonly
field (Section 10.4.2) may be used instead.
Annoyingly, this is compounded by the fact that attributes have certain restrictions too - see the C# language spec, §17.2 (again, emphasis mine):
An expression E is an attribute-argument-expression if all of the following statements are true:
The type of E is an attribute parameter type (Section 17.1.3).
At compile-time, the value of E can be resolved to one of the following:
A constant value.
A System.Type object.
A one-dimensional array of attribute-argument-expressions.
Where §17.1.3: "Attribute parameter types" says1:
The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
- One of the following types:
bool
,byte
,char
,double
,float
,int
,long
,short
,string
.- The type
object
.- The type
System.Type
.- An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility (§17.2).
- Single-dimensional arrays of the above types.
1: the quoted text is from an older version of the C# specification - in the C# 5.0 version, four additional types are mentioned:
sbyte
,uint
,ulong
, andushort
.
In other words, the best you can do is something like:
[TestFixture] public class SomeObjectTests { private static readonly SomeObject item0 = new SomeObject(0.0); private static SomeObject getObject(string key) { if ( key == "item0" ) return item0; throw new ArgumentException("Unknown key"); } [TestCase("item0", ExpectedResult = 0.0)] public double TestSomeObjectValue(string key) { SomeObject so = getObject(key); return so.Value; } [TestCase("item0", ExpectedResult = "0.0")] public string TestSomeObjectValueString(string key) { SomeObject so = getObject(key); return so.ValueString; } }
This way, the arguments to the attributes are compile-time constant, and the
getObject
method can handle getting theSomeObject
instance.这篇关于NUnit的3.0 TestCase的const的自定义对象参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!