I recently attempted to use the method Assert.Equals() when writing a new NUnit test. Upon execution this method throws an AssertionException stating that
Assert.Equals should not be used for Assertions. This is a bit baffling at first glance. What’s going on here?
I recently attempted to use the method Assert.Equals() when writing a new NUnit test.
Share
Assertis a static class inheriting from System.Object, as all classes do implicitly in C#. System.Object implements the following method:The methods on Assert which are intended for equality comparison are the
Assert.AreEqual()methods. Therefore, calling theObject.Equals()method through the Assert class in a unit test is certainly a mistake. In order to prevent this mistake and avoid confusion, the developers of NUnit have intentionally hiddenObject.Equalsin the Assert class with an implementation that throws an exception. Here’s the implementation:Of course the exception message itself is confusing, but at least it lets you know you’ve done something wrong.