Link to home
Start Free TrialLog in
Avatar of Allan
AllanFlag for United States of America

asked on

Unit Test Comparing Results of List

Hi Experts!

Thanks for reading this.

For MS Test project how would you compare that the two List of SomeModel have the same contents?

In the Test Method we have this:

List<SomeModel> expected = CreateExpectedSomeModel();
List<SomeModel> actual;

//If this was an array would use SequenceEqual
Assert.IsTrue(expected.SequenceEqual(actual));

TIA!
Avatar of kaufmed
kaufmed
Flag of United States of America image

Use the CollectionAssert class. You can use the AreEqual method to check for the same elements, in the same order; you can use the AreEquivalent method to check for the same elements, but in any order.
Avatar of Allan

ASKER

Thanks kaufmed; I'll try it later .. I'm really heading out now ....
Avatar of Allan

ASKER

Tried AreEqual and this came up:

CollectionAssert.AreEqual failed. (Element at index 0 do not match.

Tried AreEquivalent failed and this came up:

CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of <Blah.DomainClasses.SomeModel>. The actual collection contains 0 occurrence(s).

Manually inspected the contents of expected and actual (setting breakpoint) and the contents are the same.

Dumped contents of expected and actual to separate db tables and the contents are the same.

If it's any help here are the contents from the Intermediate windows during debugging:

?actual[0]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "0002-0800"
    SDATE: "19990201"
?expected[0]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "0002-0800"
    SDATE: "19990201"
    
?actual[1]
{Blah.DomainClasses.SomeModel}
    _Edate: "20221231"
    EDATE: "20221231"
    NEKEY: "52533-001"
    SDATE: "20100802"
?expected[1]
{Blah.DomainClasses.SomeModel}
    _Edate: "20221231"
    EDATE: "20221231"
    NEKEY: "52533-001"
    SDATE: "20100802"

?actual[2]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "24488-003"
    SDATE: "20110523"
?expected[2]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "24488-003"
    SDATE: "20110523"

?actual[3]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "24488-005"
    SDATE: "20100406"
?expected[3]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "24488-005"
    SDATE: "20100406"

?actual[4]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "54365-400"
    SDATE: "20000714"
?expected[4]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "54365-400"
    SDATE: "20000714"

?actual[5]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "52533-024"
    SDATE: "20110726"
?expected[5]
{Blah.DomainClasses.SomeModel}
    _Edate: "20781231"
    EDATE: "20781231"
    NEKEY: "52533-024"
    SDATE: "20110726"

?actual[6]
{Blah.DomainClasses.SomeModel}
    _Edate: "20170601"
    EDATE: "20170601"
    NEKEY: "0002-1200"
    SDATE: "20120601"
?expected[6]
{Blah.DomainClasses.SomeModel}
    _Edate: "20170601"
    EDATE: "20170601"
    NEKEY: "0002-1200"
    SDATE: "20120601"

Open in new window



Any ideas?
Avatar of Allan

ASKER

Even if we do this:

List<SomeModel> expected = CreateExpectedSomeModel();
List<SomeModel> actual = CreateExpectedSomeModel();

And try to compare with either:

CollectionAssert.AreEquivalent(expected, actual);
CollectionAssert.AreEqual(expected, actual);


we still get the same errors.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allan

ASKER

Thanks kaufmed; I'll try it when i get in..
Avatar of Allan

ASKER

Thanks again kaufmed; really appreciate your help!!!!