【发布时间】:2023-03-05 11:15:01
【问题描述】:
我有两个单元测试如下:
[DataTestMethod]
[DataRow("input1", new object[] { "input2" })]
public void Test1(string input1, object[] input2)
{
}
[DataTestMethod]
[DataRow(new object[] { "input1" }, "input2")]
public void Test2(object[] input1, string input2)
{
}
第一个单元测试Test1 不会运行,因为System.ArgumentException: Object of type 'System.String' cannot be converted to type 'System.Object[]'.。 Test2(切换参数)运行良好。我认为这可能是因为Test1 将input2 感知为一种params。 由于我没有指定 (it seems that params,所以我不确定这是怎么回事。DataRowAttribute specifies params) 有什么方法可以让Test1 工作(使用参数按那个顺序)?
正如乔纳森建议的那样,我尝试了以下方法,结果相同:
[DataTestMethod]
[DataRow("input1", (object)new object[] { (object)"input2" })]
public void Test1(string input1, object[] input2)
{
}
【问题讨论】:
-
@JonathonChase 请注意我的编辑。我没有在方法中指定
params,所以这是一个不同的问题(另外-您链接的问题的解决方案不能解决我的问题) -
不是有
params的方法,而是你使用的DataRowAttribute的构造函数。我会尝试[DataRow("input1", new [] {"input2"})]或可能的[DataRow("input1", new object[] { new object[] { "input2" } })] -
很高兴知道。请注意我的编辑
-
您的第一个建议
new [] {"input2"}会导致构建错误,而第二个建议有效,但看起来很丑陋。有没有更好的办法?
标签: c# visual-studio unit-testing