Fluent Assertions:两个数组列表的等价性,数组需要严格排序
Fluent Assertions: Equivalency of two lists of arrays, with arrays needing strict ordering
我正在尝试使用 Fluent Assertions 来测试排列生成算法。该算法生成一个 List<int[]>
,其中 List
顺序无关紧要,但每个 int[]
的元素都重要。
[Fact]
public void Are_all_permutations_generated()
{
// Arrange
var expected = new List<int[]>
{
new[] { 1, 2, 3 },
new[] { 1, 3, 2 },
new[] { 2, 1, 3 },
new[] { 2, 3, 1 },
new[] { 3, 1, 2 },
new[] { 3, 2, 1 }
};
// Act
var result = new List<int[]>
{
new[] { 3, 2, 1 },
new[] { 1, 3, 2 },
new[] { 2, 3, 1 },
new[] { 2, 1, 3 },
new[] { 3, 1, 2 },
new[] { 1, 2, 3 }
};
// Assert
result.Should().BeEquivalentTo(expected);
}
如果我在上面的代码块中使用 result.Should().BeEquivalentTo(expected)
,即使 result
是
,它也会通过
var result = new List<int[]>
{
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 }
};
我如何编写 Fluent Assertions 以允许对列表进行任何排序,但对数组进行严格排序,以便它可以断言已找到所有排列?有没有办法在 BeEquivalentTo
中写 options
来做到这一点?
您可以尝试在数组断言上使用WithStrictOrdering
。
例如:
result.Should().BeEquivalentTo(expected, options => options.WithStrictOrdering());
您还可以查看来自 FluentAssertion 的 collection assertion docs,它们提供了更多关于如何比较集合的选项。
使用 WithStrictOrderingFor
确定何时要使用严格排序。它需要一个让您可以访问 IObjectInfo
的 lambda,一个公开您可以使用的各种相关信息的对象。像
WithStrictOrderingFor(info => info.RuntimeType == typeof(int[]))
我正在尝试使用 Fluent Assertions 来测试排列生成算法。该算法生成一个 List<int[]>
,其中 List
顺序无关紧要,但每个 int[]
的元素都重要。
[Fact]
public void Are_all_permutations_generated()
{
// Arrange
var expected = new List<int[]>
{
new[] { 1, 2, 3 },
new[] { 1, 3, 2 },
new[] { 2, 1, 3 },
new[] { 2, 3, 1 },
new[] { 3, 1, 2 },
new[] { 3, 2, 1 }
};
// Act
var result = new List<int[]>
{
new[] { 3, 2, 1 },
new[] { 1, 3, 2 },
new[] { 2, 3, 1 },
new[] { 2, 1, 3 },
new[] { 3, 1, 2 },
new[] { 1, 2, 3 }
};
// Assert
result.Should().BeEquivalentTo(expected);
}
如果我在上面的代码块中使用 result.Should().BeEquivalentTo(expected)
,即使 result
是
var result = new List<int[]>
{
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 },
new[] { 1, 2, 3 }
};
我如何编写 Fluent Assertions 以允许对列表进行任何排序,但对数组进行严格排序,以便它可以断言已找到所有排列?有没有办法在 BeEquivalentTo
中写 options
来做到这一点?
您可以尝试在数组断言上使用WithStrictOrdering
。
例如:
result.Should().BeEquivalentTo(expected, options => options.WithStrictOrdering());
您还可以查看来自 FluentAssertion 的 collection assertion docs,它们提供了更多关于如何比较集合的选项。
使用 WithStrictOrderingFor
确定何时要使用严格排序。它需要一个让您可以访问 IObjectInfo
的 lambda,一个公开您可以使用的各种相关信息的对象。像
WithStrictOrderingFor(info => info.RuntimeType == typeof(int[]))