I have a problem using Fluent Assertions to compare two collections of type List<List<string>>
. When using the Should().Equal()
method (order is important) I get the following (cryptic ;-) message:
Expected collection to be equal to {{"Telefoonnummer"}, {"E-mailadres"}, {"E-mailadres controle"}}, but {{"Telefoonnummer"}, {"E-mailadres"}, {"E-mailadres controle"}} differs at index 0.
So, the objects appear to be equal. Also when debugging the objects appear to be exactly the same. When comparing two List<string>
objects the test passes, no problems, but the same test with List<List<string>>
fails. Am I using the wrong assertion method? Or does Fluent Assertions not handle this type of collection correctly?
Instead of Should().Equal()
use actualList.ShouldBeEquivalentTo(expectedList. config => config.WithStrictOrder());
ShouldBeEquivalentTo
method will check that both lists contains items with same values. In cases when list contains instance of reference types, it will compare the full object graph of those instances. Adding as a second parameter
config => config.WithStrictOrdering()
will check that order of items is same as in expected list.
Should().Equal()
on other hand will use "standard" equality check, where var listOne = new List<string> { "test" };
and var listTwo = new List<string> { "test" };
will be different instances.