Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of `ShouldBeEquivalentTo`, `ShouldAllBeEquivalentTo`, and `BeEquivalentTo`

I am working with fluent assertions in my unit tests, however the use of ShouldBeEquivalentTo, ShouldAllBeEquivalentTo, and BeEquivalentTo is unclear.

For example; all the following statements pass so the functions appear equivalent.

List<string> a = new List<string>() { "james", "wood" };
List<string> b = new List<string>() { "james", "wood" };

a.ShouldBeEquivalentTo(b);
a.ShouldAllBeEquivalentTo(b);
a.Should().BeEquivalentTo(b);

Why should I use one rather than the another?

like image 625
James Wood Avatar asked Dec 19 '15 14:12

James Wood


1 Answers

ShouldBeEquivalentTo and ShouldAllBeEquivalentTo are the same and will do a recursive structural comparison. But since the behavior is highly configurable, some of the options support using property expressions to include or exclude certain members. However, the T in the expression refers to the type of the root object (List<string>) when ShouldBeEquivalentTo is used, and string if ShouldAllBeEquivalentTo is used. The former acts on the entire object whereas the latter acts on IEnumerable<T>.

Should().BeEquivalentTo() is a much simpler implementation which will just do an order-insensitive comparison using Equals. This method will be removed in the next breaking change (v5.x).

like image 50
Dennis Doomen Avatar answered Nov 05 '22 02:11

Dennis Doomen