Click here to Skip to main content
15,885,985 members
Articles / Programming Languages / C#
Tip/Trick

Assert Property Values for Two Objects or Two Lists

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
2 Mar 2016CPOL 13.4K   33   2  
Helper class to compare two class objects while unit testing

Introduction

The following code snippet is a class that contains a method named PropertyValuesAreEquals. Often while unit testing the code, we come across situations where we need to compare two objects or two lists w.r.t. their properties and values.We want to ensure if all the values of properties are same in object 1 as in object 2. This code is an implementation of the same logic that compares two objects and also two lists if objects parameter turns out to be a List. One can use this code while writing unit tests as a helper class to assert two objects, i.e., actual vs expected.

First, install NUnit through package manager to a particular project where you want to keep this class by writing the following command in package manager console.

Install NUnit package

Using the Code

Following is the helper class:

C#
using System.Collections;  
using System.Reflection;  
using NUnit.Framework;  
namespace TestsHelper  
{  
public static class AssertObjects  
{  
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 (actualValue is IList)  
AssertListsAreEquals(property, (IList)actualValue, (IList)expectedValue);  
else if (!Equals(expectedValue, actualValue))  
if (property.DeclaringType != null)  
Assert.Fail("Property {0}.{1} does not match. Expected: {2} but was: {3}", 
property.DeclaringType.Name, property.Name, expectedValue, actualValue);  
}  
}  

private static void AssertListsAreEquals(PropertyInfo property, IList actualList, IList expectedList)  
{  
if (actualList.Count != expectedList.Count)  
Assert.Fail("Property {0}.{1} does not match. Expected IList containing {2} 
elements but was IList containing {3} elements", property.PropertyType.Name, 
property.Name, expectedList.Count, actualList.Count);  
for (int i = 0; i < actualList.Count; i++)  
if (!Equals(actualList[i], expectedList[i]))  
Assert.Fail("Property {0}.{1} does not match. Expected IList with element {1} 
equals to {2} but was IList with element {1} equals to {3}", 
property.PropertyType.Name, property.Name, expectedList[i], actualList[i]);  
}  
}  
}  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect https://codeteddy.com/
India India
Akhil Mittal is two times Microsoft MVP (Most Valuable Professional) firstly awarded in 2016 and continued in 2017 in Visual Studio and Technologies category, C# Corner MVP since 2013, Code Project MVP since 2014, a blogger, author and likes to write/read technical articles, blogs, and books. Akhil is a technical architect and loves to work on complex business problems and cutting-edge technologies. He has an experience of around 15 years in developing, designing, and architecting enterprises level applications primarily in Microsoft Technologies. He has diverse experience in working on cutting-edge technologies that include Microsoft Stack, AI, Machine Learning, and Cloud computing. Akhil is an MCP (Microsoft Certified Professional) in Web Applications and Dot Net Framework.
Visit Akhil Mittal’s personal blog CodeTeddy (CodeTeddy ) for some good and informative articles. Following are some tech certifications that Akhil cleared,
• AZ-304: Microsoft Azure Architect Design.
• AZ-303: Microsoft Azure Architect Technologies.
• AZ-900: Microsoft Azure Fundamentals.
• Microsoft MCTS (70-528) Certified Programmer.
• Microsoft MCTS (70-536) Certified Programmer.
• Microsoft MCTS (70-515) Certified Programmer.

LinkedIn: https://www.linkedin.com/in/akhilmittal/
This is a Collaborative Group

780 members

Comments and Discussions

 
-- There are no messages in this forum --