Solved

Unit Test Comparing Results of List

Posted on 2013-06-17
7
1,807 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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