3

I cant seem to find out why I am getting this error as the actual and expected are returning the same values at index 0 and have exactly the same properties. what could be the possible cause of this problem? I've looked around but cant find any viable solution as of yet.

[TestMethod()]
        public void unSortedLeadsTest()
        {
            List<CustomerLead> expected = new List<CustomerLead>();
            List<CustomerLead> actual = new List<CustomerLead>();
            CustomerLeads target = new CustomerLeads(); // TODO: Initialize to an appropriate value
            string xml = "C:/Users/Admin/Downloads/potentialcustomers.xml"; // TODO: Initialize to an appropriate value

            actual = target.unSortedLeads(xml);
            CustomerLead lead = new CustomerLead()
            {
                FirstName = actual[0].FirstName,
                LastName=actual[0].LastName,
                EmailAddress=actual[0].EmailAddress

            };
            CustomerLead lead1 = new CustomerLead()
            {
                FirstName = actual[1].FirstName,
                LastName = actual[1].LastName,
                EmailAddress = actual[1].EmailAddress

            };
            CustomerLead lead2 = new CustomerLead()
            {
                FirstName = actual[2].FirstName,
                LastName = actual[2].LastName,
                EmailAddress = actual[2].EmailAddress

            };

            target.addressList.Add(lead);
            target.addressList.Add(lead1);
            target.addressList.Add(lead2);


            foreach (CustomerLead i in target.addressList) {

            expected.Add(lead);
            }

            // TODO: Initialize to an appropriate value

            CollectionAssert.AreEqual(expected, actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

Edit: Ive attempted to override Equals but am struggling: any Ideas how I might acheive this?

public override bool Equals(Object obj)
        {
            if (obj == null)
                return false;

           CustomerLead leadsequal = obj as CustomerLead;
           if ((Object)leadsequal == null)
                return false;
            else
               return Equals( leadsequal);
        }
user2708073
  • 169
  • 4
  • 15
  • Have a look at this thread, in case its relevant: http://stackoverflow.com/questions/19724524/cannot-compare-lists-in-unittests/19724980#19724980 – Matthew Watson Nov 06 '13 at 16:01

3 Answers3

9

I suspect that this:

foreach (CustomerLead i in target.addressList) {
    expected.Add(lead);
}

should be:

foreach (CustomerLead i in target.addressList) {
    expected.Add(i);
}

Otherwise you're adding the same reference three times.

It's not really clear to me what you're trying to test, mind you... and you may well be fine with just:

List<CustomerLead> expected = target.addressList.ToList();

... along with a using directive of:

using System.Linq;

EDIT: Additionally, for two objects to be considered equal just because they've got the same properties, you'll need to override object.Equals(object) and ideally implement IEquatable<CustomerLead> too. By default, you just get reference equality - any two distinct objects are considered non-equal, even if every property is equal.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the quick response. I've changed the `lead` to `i`in the foreach but still getting the same error. also `.ToList` doesnt appear for `target.addressList` – user2708073 Nov 06 '13 at 15:36
  • @user2708073: See my edit. As for why `ToList()` doesn't appear - you'd need `using System.Linq;` Of course we don't know the type of `targetList`, which doesn't help... – Jon Skeet Nov 06 '13 at 15:42
  • Thanks how do I go about implementing IEquatable? and overriding the equas(objecT)? – user2708073 Nov 06 '13 at 15:58
  • @user2708073: Well what have you *tried* when it comes to overriding `Equals`? What did you get stuck on? – Jon Skeet Nov 06 '13 at 16:49
  • I havent tried anything yet because Im not sure how this will work. because I already have `List expected = new List();` `List actual = new List();` How Will I implement Equatable? and Im not sure what I will use to override Equals. Ive done some reading but not sure I fully understand. Also, there were no examples for Collections. – user2708073 Nov 06 '13 at 16:56
  • 1
    @user2708073: It's not about collections - it's about making one `CustomerLead` equal to another, based on their properties. Read http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx – Jon Skeet Nov 06 '13 at 16:57
  • Im still not quite understanding how to do this. Can you see my edit? thanks! – user2708073 Nov 06 '13 at 17:15
  • Would It be useful if I provided the class I was testing? – user2708073 Nov 06 '13 at 17:29
  • @user2708073: Your `Equals` method should be comparing each property... and you'll need to override `GetHashCode` too. I don't really want to go any further in this comment thread - it sounds like the kind of thing where you'd be best off finding a colleague who can help you in person. – Jon Skeet Nov 06 '13 at 18:44
  • Greatly appreciate the help. – user2708073 Nov 06 '13 at 18:46
5

Firstly, as others have pointed out, you have to correctly implement Equals method.

public override bool Equals(Object obj)
{
    if (obj == null)
        return false;

    CustomerLead other = obj as CustomerLead;
    if ((Object)other == null)
        return false;

    // here you need to compare two objects
    // below is just example implementation

    return this.FirstName == other.FirstName
        && this.LastName == other.LastName
        && this.EmailAddress == other.EmailAddress;
}


Secondly, in your test method, you must not use result values from method you're testing to prepare expected collection. If unSortedLeads method has simple error and swaps FirstName with LastName you will never find such error with this test. Instead you should use literal values.

[TestMethod()]
public void unSortedLeadsTest()
{
    // read objects from xml
    string xml = "C:/Users/Admin/Downloads/potentialcustomers.xml";
    CustomerLeads target = new CustomerLeads();
    List<CustomerLead> actual = target.unSortedLeads(xml);

    // prepare expected collection
    List<CustomerLead> expected = new List<CustomerLead>()
    {
        new CustomerLead()
        {
            FirstName = "FirstName1",
            LastName = "LastName1",
            EmailAddress = "Email@Address1"
        },
        new CustomerLead()
        {
            FirstName = "FirstName2",
            LastName = "LastName2",
            EmailAddress = "Email@Address2"
        },
        new CustomerLead()
        {
            FirstName = "FirstName3",
            LastName = "LastName3",
            EmailAddress = "Email@Address3"
        }
    };

    // test equality
    CollectionAssert.AreEqual(expected, actual);
}


You can read more about implementing Equals method here and about unit testing here

mozgow
  • 197
  • 1
  • 13
0
List<AdminUser> adminDetailsExpected = new List<AdminUser>()
{
new AdminUser  {firstName = "test1" , lastName = "test1" , userId = 
"001test1"  },
new AdminUser {firstName = "test2" , lastName = "test2" , userId = 
"002test2"   }
};

 //Act


List<AdminUser> adminDetailsActual = RetrieveAdmin(); // your retrieve logic goes here

//Assert
Assert.AreEqual(adminDetailsExpected.Count, adminDetailsActual.Count);  //Test succeeds if the count matches else fails. This count can be used as a work around to test
karthik kasubha
  • 392
  • 2
  • 13