Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit - Assert to check all properties are equal? [duplicate]

Tags:

nunit

Is there an assertion built into Nunit that checks all properties between 2 objects are the same, without me having to override Equals?

I'm currently using reflection to Assert each individual property for a pair of objects.

like image 289
Castrohenge Avatar asked Jul 21 '09 08:07

Castrohenge


People also ask

What is Assert areequal()?

Tests whether the specified objects are equal and throws an exception if the two objects are not equal. Different numeric types are treated as unequal even if the logical values are equal.

How do I compare two objects in NUnit?

Testing Object Equivalence These methods test whether the same objects are referenced by the two arguments. Assert. AreSame( object expected, object actual ); Assert. AreSame( object expected, object actual, string message ); Assert.

What is assert that in NUnit?

Asserts that a condition is true. If the condition is false the method throws an AssertionException. That(Boolean, String) Asserts that a condition is true. If the condition is false the method throws an AssertionException.

Is equal to NUnit?

NUnit is able to compare single-dimensioned arrays, multi-dimensioned arrays, nested arrays (arrays of arrays) and collections. Two arrays or collections are considered equal if they have the same dimensions and if each pair of corresponding elements is equal.


2 Answers

I don't believe there is.

Assert.AreEqual compares non-numeric types by Equals.
Assert.AreSame checks if they refer to the same object

like image 65
AdaTheDev Avatar answered Oct 02 '22 06:10

AdaTheDev


You can write framework agnostic asserts using a library called Should. It also has a very nice fluent syntax which can be used if you like fluent interfaces. I had a blog post related to the same.

http://nileshgule.blogspot.com/2010/11/use-should-assertion-library-to-write.html

You can two objects and there properties with ShouldBeEquivalentTo

dto.ShouldBeEquivalentTo(customer);
like image 24
Nilesh Gule Avatar answered Oct 02 '22 08:10

Nilesh Gule