c# - 将数组作为属性值传递

标签 c# attributes nunit

我有一个测试方法如下:

[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/

相关文章:

c# attribute over main 属性

c# - 如何在 XAML 中将代码隐藏与 Hub 一起使用?

c# - 如何在 C# 中获取目录大小(目录中的文件)?

c# - LINQ 选择不同的项目和子项目

c# - 没有逗号的单行属性?

python - 维护按多个属性排序的列表?

c# - 如何在用 C# 编写的 Selenium WebDriver(Nunit 测试用例)中按 "Enter"?

nunit - 尝试在 LINQPad 中使用 TypeMock 运行 nunit 测试时出错

C# - 测试通用私有(private)函数

c# - BitmapImage OutOfMemoryException 仅在 Windows 8.1 中