如何使用 FluentAssertions 测试嵌套集合

bra*_*ing 5 c# collections tdd fluent-assertions

我有以下规格

BidirectionalGraph Fixture = new BidirectionalGraph();

public void VerticesShouldBeAbleToAssociateMultipleEdges()
{
    int a = 0;
    int b = 1;
    int c = 2;

    Fixture.AddEdge(a, b);
    Fixture.AddEdge(b, c);
    Fixture.AddEdge(c, a);

    Fixture.EdgesFrom(a).Should().BeEquivalentTo
        ( new []{a, b} 
        , new []{a, c});
}
Run Code Online (Sandbox Code Playgroud)

其中 EdgesFrom 定义如下

public IEnumerable<int[]> EdgesFrom(int vertex)
Run Code Online (Sandbox Code Playgroud)

但是我的测试失败了

Result Message: Expected collection 

    {{0, 1}, {0, 2}} to be equivalent to 
    {{0, 1}, {0, 2}}.
Run Code Online (Sandbox Code Playgroud)

这对我来说不太有意义,因为它们显然是等效的。FluentAssertions在比较集合的集合时不起作用吗?

Den*_*men 2

这是因为 collection.Should().BeEquivalentTo() 使用您类型的默认 Equals() 实现来确保第一个集合中的每个项目出现在第二个集合中的某个位置。您真正需要的是我在 Fluent Assertions 2.0 中引入的新等效功能。不幸的是,我最近才意识到令人困惑的语法(collection.Should().BeEquivalentTo() 与 ShouldAllBeEquivalentTo())。

  • 说得通。在内部,Should().BeEquivalentTo() 应该使用 ShouldBeEquivalentTo() 提供的相同结构等效 API。 (2认同)