NUnit’s new Constraint model


I was working on some code this weekend and happened to come across NUnit 2.4’s new constraint objects.  I can’t believe I just now found out about this!  I am glad to see that the mocking frameworks influenced this addition to NUnit.   Being a big fan of fluent interfaces, I love how the new constraint model allows the assertion model to be easily read.  For example:

This was the tried and true Assertion model for the past 4 years.

Assert.AreEqual(expectedBallance, savings.Ballance);

Now with the new constraint model the assertion can now be written as:

Assert.That(savings.Ballance, Is.EqualTo(expectedBallance));

 

I will start refactoring my unit test to this syntax going forward.  I encourage everyone else to do the same as well.

If you want to see the different uses of the constraint model you can view the NUnit source code and look for the class: AssertSyntaxTests

or you can click here.

The Jing Project