【发布时间】:2019-11-14 20:31:57
【问题描述】:
我将测试返回二维数组中单元格的冯诺依曼邻居数组的函数。 单元格包含有关模拟的数据。
为了进行测试,我设置了新的 Cell[,] 并填充了 Cell 实例。 测试应该检查函数返回的邻居是否与预期的实例相同,并且在数组中的顺序相同。
public class VonNeumanNeighbourhoodTest
{
private static Cell a,b,c,d,e,f,g,h,i ;
private static Cell[,] space;
public VonNeumanNeighborhoodTest() {
a = new Cell{ GrainMembership = new Grain(0, Color.Red) };
b = new Cell{ GrainMembership = new Grain(1, Color.Green) };
// And so on
i = new Cell{ GrainMembership = new Grain(8, Color.Azure) };
space = new Cell[3, 3]
{
{ a, b, c },
{ d, e, f },
{ g, h, i }
};
}
问题出现在测试方法中。 Cell[] expected 在调试中总是包含 {null, null, null, null} 而不是 eg.{b, f, h, d} 参考。
[Theory]
[ClassData(typeof(AbsorbingTestData))]
public void AbsorbingTest(int x, int y, Cell[] expected)
{
var neighbours = VonNeumanNeighbourhood.Neighbours(space , x, y, AbsorbingBoundary.BoundaryCondition);
for(int i = 0; i < 4; i++)
{
Assert.Same(neighbours[i], expected[i]);//Checking if neighbours and expected are this same instances
}
}
}
private class AbsorbingTestData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[] { 1, 1, new Cell[]{b, f, h, d} }; //e - center
yield return new object[] { 0, 0, new Cell[]{null, b, d, null} }; //a
//More cases
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
我正在尝试使用 [MemberData] 属性的类似代码,但结果相同。
【问题讨论】: