Solved

Unit Test Comparing Results of List

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

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
Thanks kaufmed; I'll try it later .. I'm really heading out now ....
0
 
LVL 5

Author Comment

by:allanau20
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Author Comment

by:allanau20
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
Thanks kaufmed; I'll try it when i get in..
0
 
LVL 5

Author Closing Comment

by:allanau20
Comment Utility
Thanks again kaufmed; really appreciate your help!!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now