我想用 List<List<string>>
测试一个方法作为参数。我使用 xunit 作为测试框架。
这是我尝试过的。
public static IEnumerable<IEnumerable<string>> CombinationData
{
get
{
return new List<List<string>>
{
new List<string>{ "a", "b", "c" },
new List<string>{ "x", "y", "z" }
};
}
}
[Theory]
[MemberData(nameof(CombinationData))]
public void CombinationDataTest(List<List<string>> dataStrings)
{
...
}
运行测试时出现以下异常。
System.ArgumentException :
Property CombinationData on CodeUnitTests.MainCodeTests yielded an item that is not an object[]
Stack Trace: at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
我该如何让它发挥作用?这是正确的方法吗?
最佳答案
错误信息非常清楚。提供的功能为MemberData
应该返回IEnumerable<object[]>
,其中
IEnumerable
的每一项是一个测试用例的参数集合- 每个
object
在object[]
是测试方法期望的参数
您的测试方法期望 List<List<string>>
作为参数,因此您应该返回 List<List<string>>
的实例作为 object[]
的第一项
private static IEnumerable<object[]> CombinationData()
{
var testcase = new List<List<string>>
{
new List<string>{ "a", "b", "c" },
new List<string>{ "x", "y", "z" }
};
yield return new object[] { testcase };
}
关于c# - 如何在 Xunit 中测试使用 List<List<T> 作为输入参数的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45947690/