我有一个测试方法如下:
[TestCase(new string[] { "1", "2", "5" }, Result = true)]
bool AllocateIDsTest1(IEnumerable<string> expected)
{
var target = ...
var actual = target.AllocateIDs(expected);
return actual.SequenceEqual(expected);
}
但是我得到一个编译器错误:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.
编译器可能无法区分以下构造函数:
TestCase(params object[] args, Named Parameters);
和
TestCase(object ob1, Named Paramaters);
因为 new string[] { "1", "2", "5"}
可以解析为 params object[]
和 object
.
来自 this post我知道字符串数组应该可以作为编译常量传递。
如何向TestCase
提供字符串数组?
最佳答案
我找到了一个使用参数方法的解决方案:
[TestCase("1", "2", "5", Result = true)]
public bool AllocateIDsTest1(params string[] expected)
{
var target = ...
var actual = target.AllocateIDs(expected);
return actual.SequenceEqual(expected);
}
关于c# - 将数组作为属性值传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38304465/