Solved

Unit Test Comparing Results of List

Posted on 2013-06-17
7
1,763 Views
Last Modified: 2013-06-19
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!
0
Comment
Question by:allanau20
  • 5
  • 2
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39255171
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.
0
 
LVL 5

Author Comment

by:allanau20
ID: 39255176
Thanks kaufmed; I'll try it later .. I'm really heading out now ....
0
 
LVL 5

Author Comment

by:allanau20
ID: 39257872
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?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 5

Author Comment

by:allanau20
ID: 39258100
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.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39258469
Hmmm...  The documentation for AreEquivalent seems to betray its actual operation. I'm not certain now that it will provide you with what you need.

Nevertheless, there is an overload of the AreEqual method that can take in an instance of an IComparer. You can use this overload to have AreEqual compare the properties of the classes that are within your list.

For example:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections;
using System.Collections.Generic;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        public class Example
        {
            public string Property { get; set; }
        }

        public class ExampleComparer : IComparer<Example>, IComparer
        {
            public int Compare(Example x, Example y)
            {
                return x.Property.CompareTo(y.Property);
            }

            public int Compare(object x, object y)
            {
                Example castX = x as Example;
                Example castY = y as Example;

                if (castX == null || castY == null)
                {
                    return 0;
                }

                return Compare(castX, castY);
            }
        }

        [TestMethod]
        public void TestMethod1()
        {
            List<Example> expected = new List<Example>
            {
                new Example() { Property = "one" },
                new Example() { Property = "two" },
                new Example() { Property = "three" },
            };
            List<Example> actual = new List<Example>
            {
                new Example() { Property = "one" },
                new Example() { Property = "two" },
                new Example() { Property = "three" },
            };

            CollectionAssert.AreEqual(expected, actual, new ExampleComparer());
            //CollectionAssert.AreEqual(expected, actual);  // uncomment to see behavior when you don't use a Comparer
        }
    }
}

Open in new window

0
 
LVL 5

Author Comment

by:allanau20
ID: 39258515
Thanks kaufmed; I'll try it when i get in..
0
 
LVL 5

Author Closing Comment

by:allanau20
ID: 39260768
Thanks again kaufmed; really appreciate your help!!!!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question