Skip to content

Test Extensions

GregFinzer edited this page Dec 21, 2017 · 1 revision

Compare .NET Objects has two extensions for usage with NUnit, MSTest, etc.

ShouldCompare

It throws an exception if the objects are not equal. Optionally pass in an error message to be displayed at the top.

[Test]
public void ShouldCompare_When_Equal_Should__Not_Throw_An_Exception()
{
	//Arrange
	string errorMessage = "Groups should be equal";
	var people1 = new List<Person>() { new Person() { Name = "Joe" } };
	var people2 = new List<Person>() { new Person() { Name = "Joe" } };
	var group1 = new KeyValuePair<string, List<Person>>("People", people1);
	var group2 = new KeyValuePair<string, List<Person>>("People", people2);

	//Assert
	group1.ShouldCompare(group2, errorMessage);
}

ShouldNotCompare

It throws an exception if the objects are equal. Optionally pass in an error message to be displayed at the top.

[Test]
public void ShouldNotCompare_When_Not_Equal_Should__Not_Throw_An_Exception()
{
	//Arrange
	string errorMessage = "Groups should be equal";
	var people1 = new List<Person>() { new Person() { Name = "Joe" } };
	var people2 = new List<Person>() { new Person() { Name = "Sue" } };
	var group1 = new KeyValuePair<string, List<Person>>("People", people1);
	var group2 = new KeyValuePair<string, List<Person>>("People", people2);

	//Act
	group1.ShouldNotCompare(group2, errorMessage);
}

Specifying the configuration

CompareExtensions.Config.CaseSensitive = false;