Question

I'm trying to compare 2 objects expectedItems and resultItems of type IEnumerable<IDictionary<string, object>> but haven't been able to do much with it.

Also, one dictionary is initialised in the code, and the other one is built from a JSON response from an external API. Since, JSON doesn't really care about the order of the properties within the object, a SequenceEquals is ruled out.

I do have two equal objects and all of these understated methods are failing,
First,

CollectionAssert.AreEqual(expectedItems, resultItems)

Second,

var expectedItems = entries.Select(e => e.Serialize()).ToList();
resultItems.Zip(expectedItems,(objects, dictionary) => 
objects.OrderBy(pair =>pair.Key).SequenceEqual(dictionary.OrderBy(pair => pair.Key)))
                                   .Should()
                                   .NotContain(false);

The objects (as far as I see) are equal.

enter image description here

Any thing that I can try, or anything that I am currently doing wrong?

EDIT
The API trims the tick count from the timestamp that's why the failure. How can I trim the timestamp in the expectedItems dictionary and then compare? So, the collections have to be same and the comparison for timestamp needs to be overridden. Anyone? enter image description here

Was it helpful?

Solution

Two things to check:

  1. Are the object implementing preperly the Equals() method? If not the equality just check for the reference, and since they are two different instance they appear to be different. Another option could be, since your object is representing a Timestamp, as I guess by the picture.
  2. even if it properly implements Equals, are the objects the same even in term of the millisecond ( if existing ) portion?

OTHER TIPS

Why don't you install FluentAssertions and do something like this?

resultsItem.ShouldAllBeEquivalentTo(expectedItems);

I don't know by heart how restrictive the DateTime comparison is, but you can easily override the behavior for a particular property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top