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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With