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.


Posted Jul 22 2007, 09:48 PM by Joe Ocampo

Comments

DotNetKicks.com wrote NUnit's new Constraint model
on 07-22-2007 9:54 PM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Jason Meridth wrote re: NUnit's new Constraint model
on 07-23-2007 4:23 AM

Awesome find.  I guess we'll start using this in the lab? =)

Ben Scheirman wrote re: NUnit's new Constraint model
on 07-23-2007 8:28 AM

That is worlds easier to read!  Too bad I'll have to wait a year before MS Test "invents" that feature.

Joe Ocampo wrote re: NUnit's new Constraint model
on 07-23-2007 1:38 PM

Jason, read my mind!

Joe Ocampo wrote re: NUnit's new Constraint model
on 07-23-2007 1:41 PM

Ben,

I encourage you an everyone else who is interested to contact Naysawn Naderi (program manager for Team Systems Testing) about including such language constructs in their testing suite.

http://blogs.msdn.com/nnaderi/

Maybe we can make a difference!

Judah wrote re: NUnit's new Constraint model
on 08-08-2007 7:43 AM

Thanks - I can't believe I haven't heard anything about this either. I'll be encouraging my team to use these.

Troy DeMonbreun wrote re: NUnit's new Constraint model
on 08-20-2007 11:35 AM

I just wrote an article that covers most of the Constraints' syntax:

<a href="blog.troyd.net/PermaLink,guid,21b4cfe8-9d82-443f-86a5-db2d93d6e617.aspx">Learn the new NUnit 2.4 Constraint-Based Assert Model</a>

Thanks!

Troy DeMonbreun wrote re: NUnit's new Constraint model
on 08-20-2007 11:46 AM

I just wrote an article that covers most of the Constraints' syntax:

blog.troyd.net/PermaLink,guid,21b4cfe8-9d82-443f-86a5-db2d93d6e617.aspx

Thanks!

Shallow Dive with BDD « Convex Corners wrote Shallow Dive with BDD &laquo; Convex Corners
on 06-08-2008 3:49 AM

Pingback from  Shallow Dive with BDD &laquo; Convex Corners

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems