Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Unit Test Comparing Results of List

Posted on 2013-06-17
7
Medium Priority
?
2,022 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

661 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