将多个数组传递给 xUnit
Pass Multiple Arrays to xUnit
我了解如何将一个数组传递给 xUnit:Pass array of string to xunit test method
但是,我想将三个数组传递给我的测试方法:
Type1[] ReturnValuesForMock;
Type2[] ExpectedTestResult;
Type3[] TestData
有什么办法吗? Type1
是一个枚举,所以我可以使用编译时常量 new [] {enum}
但这对需要调用 new()
的 Type2
不起作用。然后我可以将 Type3
处理为 params
.
我觉得这真的应该是可能的,但我不确定如何...
我不确定这是否是最好的方法,但我发现我可以像这样在我的测试中使用 [ClassData]
属性:
[Theory]
[ClassData(typeof(TestDataGenerator))]
public void Test1(Type1[] t1, Type2[] t2, Type3[] t3)
{
//MyTest
}
然后通过TestDataGenerator
class:
提供数据
private class TestDataGenerator:IEnumerable<object[]>
{
private static readonly List<object[]> _data = new()
{
new object[]{new Type1[] {...}, new Type2[]{...)}, new Type3[]{...}},
...
};
public IEnumerator<object[]> GetEnumerator() => _data.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
我了解如何将一个数组传递给 xUnit:Pass array of string to xunit test method
但是,我想将三个数组传递给我的测试方法:
Type1[] ReturnValuesForMock;
Type2[] ExpectedTestResult;
Type3[] TestData
有什么办法吗? Type1
是一个枚举,所以我可以使用编译时常量 new [] {enum}
但这对需要调用 new()
的 Type2
不起作用。然后我可以将 Type3
处理为 params
.
我觉得这真的应该是可能的,但我不确定如何...
我不确定这是否是最好的方法,但我发现我可以像这样在我的测试中使用 [ClassData]
属性:
[Theory]
[ClassData(typeof(TestDataGenerator))]
public void Test1(Type1[] t1, Type2[] t2, Type3[] t3)
{
//MyTest
}
然后通过TestDataGenerator
class:
private class TestDataGenerator:IEnumerable<object[]>
{
private static readonly List<object[]> _data = new()
{
new object[]{new Type1[] {...}, new Type2[]{...)}, new Type3[]{...}},
...
};
public IEnumerator<object[]> GetEnumerator() => _data.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}