gpt4 book ai didi

c# - 如何将二维数组设置为单元测试的参数

转载 作者:行者123 更新时间:2023-12-04 01:35:13 27 4
gpt4 key购买 nike

如果预期的变量是整数,它就像这样

[DataRow(2)]
[TestMethod]
public void TestMethod(int expected)
{
// some code...
}

但是当有二维数组 int[,] 而不是 int 参数时应该怎么办?当我尝试这样做时

[DataRow(new int[,] { {0, 0}, {0, 0} })]
[TestMethod]
public void TestMethod(int[,] expected)
{
// some code...
}

错误说

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

最佳答案

您可以使用 DynamicData Attribute 来实现它如下所示:

[DataTestMethod]
[DynamicData(nameof(TestDataMethod), DynamicDataSourceType.Method)]
public void TestMethod1(int[,] expected)
{
// some code...
var b = expected;
}

static IEnumerable<object[]> TestDataMethod()
{
return new[] { new[] { new int[,] { { 0, 0 }, { 1, 1 } } } };
}

输出

enter image description here

关于c# - 如何将二维数组设置为单元测试的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59806518/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com