Reputation: 169
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);
}
Upvotes: 3
Views: 9610
Reputation: 424
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
Upvotes: 0
Reputation: 197
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
Upvotes: 5
Reputation: 1499770
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.
Upvotes: 9