Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this Assert fail?

IEnumerable<ReportReceipt> expected = new List<ReportReceipt>()
{
    new ReportReceipt("fileName1","Hash1","some comments1")
};

IEnumerable<ReportReceipt> actual = new List<ReportReceipt>()
{
    new ReportReceipt("fileName1","Hash1","some comments1")
};

Assert.IsTrue(expected.SequenceEqual(actual));

I'm running MSTest with VS 2008.

like image 458
Peter Goras Avatar asked Jan 21 '23 22:01

Peter Goras


2 Answers

SequenceEqual determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.

If you haven't overloaded the Equals and GetHashCode in your class, the fallback object equality check will fail, since the sequences contain two different objects.

like image 109
Mia Clarke Avatar answered Jan 27 '23 21:01

Mia Clarke


Presumably because ReportReceipt doesn't override Equals - so it's comparing just the references, and they're unequal.

Override Equals and GetHashCode appropriately and it should work fine.

like image 22
Jon Skeet Avatar answered Jan 27 '23 22:01

Jon Skeet