5

I have the following spec

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});
}

where EdgesFrom is defined so

public IEnumerable<int[]> EdgesFrom(int vertex)

however my test fails with

Result Message: Expected collection 

    {{0, 1}, {0, 2}} to be equivalent to 
    {{0, 1}, {0, 2}}.

Which doesn't quite makes sense to me as they are obviously equivalent. Does FluentAssertions just not work when comparing collections of collections?

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217

1 Answers1

2

That's because collection.Should().BeEquivalentTo() uses the default Equals() implementation of your type to make sure each item in the first collection appears somewhere in the 2nd collection. What you really need is new equivalency feature that I introduced in Fluent Assertions 2.0. Unfortunately I only recently became aware of confusing syntax (collection.Should().BeEquivalentTo() versus ShouldAllBeEquivalentTo()).

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44
  • 1
    Naively I would assume that Should().BeEquivalentTo() would do nested structural equality testing. How about Should().BeEquivalentTo(depth:-1) for infinite depth and let depth take a default value of ``1`` to keep backward compatibility. – bradgonesurfing May 10 '13 at 19:59
  • 2
    Makes sense. Internally, Should().BeEquivalentTo() should use the same structural equivalence API that ShouldBeEquivalentTo() offers. – Dennis Doomen May 11 '13 at 08:59