How to compare lists using fluent-assertions?
Not right now. We do have the new equivalency assertion syntax of FA 2.0, but that will also verify if the objects appear in the right order. For FA 2.1 I'm trying to support that, but I'm not sure yet if that will work. It basically means it has to compare the entire object graph behind a collection item with the object graphs for each and every other item in the collection. Surely it will be rather slow.
Related videos on Youtube
setebos
Updated on July 09, 2022Comments
-
setebos almost 2 years
I want to compare a list of objects, ignoring the order of the objects in the list and only comparing some of the properties in the objects, currently I'm using the following code to perform this comparison:
actual.Should().NotBeNull(); actual.Count.Should().Be(expected.Count); //compare ignoring order foreach (var exp in expected) actual.Should().Contain(act => act.IndividualId.Equals(exp.IndividualId) && act.Email.Equals(exp.Email) && act.FirstName.Equals(exp.FirstName) && act.LastName.Equals(exp.LastName) );
However this seems less than ideal, as when there is a failure you do not get a print out of the expected values. Is there a built in mechanism for performing this comparison using fluent assertions?
-
BraveNewMath about 10 yearsIs it possible say compare two List<string> types using actual.Should().Contain(expected)?
-
Dennis Doomen about 10 yearsYes, you can do actual.Should().BeEquivalentTo(expected);
-
Choco Smith over 9 yearsfor flexibility we usually serialize each object to json then use string compare, the error output looks good to and you never need to update your unit test if someone adds a new property
-
Alexander Abakumov over 9 years@DennisDoomen, Are there any new approaches in the current FA?
-
Dennis Doomen over 9 yearsIn version 2.1 of FA we started to ignore ordering of items by default. Is that what you mean?