I downloaded NUnit 2.4.1 at the beginning of June. As always, I don’t read any of the new documentation. What was I thinking? I should have read it before now.
For people who are using NUnit, you use the Assert class where you compare the expected result to the actual value. While this works very well, the new way to write your validations with the NUnit API is a notch above. They call it the constraint-based assert model. It uses one method on the Assert class called That().
Under the classic model, you would write:
Assert.AreEqual(expectedResult, actualResult);
But under the constraint model, it goes like this:
Assert.That(expectedResult, Is.EqualTo(actualResult);
What I love about the new model is its readability. It sounds like a sentence. While writing it, I was thinking of the phrase “I assert that my expected result is equal to my actual result”. And that’s exactly what came out. Thing a beauty!
Let’s say you want to assert the expected result is not the same as the actual result.
Classic model
Assert.AreNotEqual(expectedResult, actualResult);
While in the constraint model, you would write
Assert.That(expectedResult, Is.Not.EqualTo(actualResult));
It’s so easy to write and understand. Here are a few more examples to wet your appetite:
Assert.That(expectedResult, Is.Null);
Assert.That(expectedBooleanResult, Is.True);
Assert.That(expectedStringResult, Is.Empty);
I thought that NUnit had reached its goal. That you couldn’t make
it any better. It served one purpose, make unit tests, and do it
well. I was wrong.
One
last note is that you must put the statement using
NUnit.Framework.SyntaxHelpers at the top of your class to use the Is class.
Constraint-Based Assert model: http://www.nunit.org/index.php?p=constraintModel&r=2.4.1