如何在 Xunit 中使用 List<List<T> 作为输入参数测试方法
How to test a method with List<List<T> as input parameter in Xunit
我想测试一个以 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
提供的函数应该 return IEnumerable<object[]>
,其中
IEnumerable
的每一项都是一个测试用例的参数集合
object[]
中的每个object
都是测试方法期望的参数
您的测试方法需要 List<List<string>>
作为参数,因此您应该 return 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 };
}
我想测试一个以 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
提供的函数应该 return IEnumerable<object[]>
,其中
IEnumerable
的每一项都是一个测试用例的参数集合object[]
中的每个object
都是测试方法期望的参数
您的测试方法需要 List<List<string>>
作为参数,因此您应该 return 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 };
}