Reputation: 31
I'm fairly new to unit testing. The following code is just for reference. I want to check empId of list one is same as the emp id of list 2 or not.
public class EmpInfo
{
public EmpInfo( string lastName,string firstName, string empId)
{
EAlphabeticLastName = lastName;
EFirstName = firstName;
EmpId = empId;
}
}
[Test]
[Category("Explicit")]
public void testEmp()
{
public List<EmpInfo> List1e = new List<EmpInfo>(){
new EmpInfo("dx","Tex","25")
};
public List<EmpInfo> List2e = new List<EmpInfo>(){
new EmpInfo("dx","Tex","25")
};
Assert.AreEqual(List1e.empId,List2e.empId);
}
What is the correct way to check equality of list items in Nunit (C#) ?
Upvotes: 1
Views: 2347
Reputation: 14198
There are numerous ways to achieve it
List1e.Should().BeEquivalentTo(List2e);
Move all the individual comparisons to the .Equals
method (Or implement IEqualityComparer
)
Build a helper method that iterates through public properties by reflection and assert each property
public static void PropertyValuesAreEquals(object actual, object expected) {
PropertyInfo[] properties = expected.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
object expectedValue = property.GetValue(expected, null);
object actualValue = property.GetValue(actual, null);
if (!Equals(expectedValue, actualValue))
Assert.Fail("Property {0}.{1} does not match. Expected: {2} but was: {3}", property.DeclaringType.Name, property.Name, expectedValue, actualValue);
//……………………………….
}
public static void AreEqualByJson(object expected, object actual)
{
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var expectedJson = serializer.Serialize(expected);
var actualJson = serializer.Serialize(actual);
Assert.AreEqual(expectedJson, actualJson);
}
Upvotes: 1
Reputation: 13681
Alternatives to checking that the list contains only items with the same ID:
Implement equality for the EmpInfo
class, as suggested by User965207, either by overriding Equals
or implementing the IEquatable
interface. NUnit will use either one if present. If you don't have control over the code of the system under test, this is of course not available to you.
Use a Select
statement to create a new list for comparison, as suggested by Sean. You actually only need to do this for the actual value being tested. Your expected value can just be a list or array of empids in the first place. This approach only requires changing the test code.
Use NUnit's List
class to do essentially the same as option 2 for you...
Assert.That(List.Map(List1e).Property("empid"), Is.EqualTo(new [] {"empid1", "empid2" /etc/}));
Since you have not provided a full implementation of EmpInfo
, I made the assumption that empid
is a property. If it's a field, this won't work.
I realize I haven't added a lot to the prior answers here, but I thought a summary of the possible approaches with pros and cons would be of help.
Upvotes: 1
Reputation: 1685
You may have to override the Equals
and GetHashCode
method in the class EmpInfo
and write the compare logic .
Use the above methods to check whether all the objects in one list are present in another .
Upvotes: 1