in

 

AgileJoe

Answering all world issues with, "...it depends..."

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.

Comments

 

DotNetKicks.com said:

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

July 22, 2007 9:54 PM
 

Jason Meridth said:

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

July 23, 2007 4:23 AM
 

Ben Scheirman said:

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

July 23, 2007 8:28 AM
 

Joe Ocampo said:

Jason, read my mind!

July 23, 2007 1:38 PM
 

Joe Ocampo said:

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!

July 23, 2007 1:41 PM
 

Judah said:

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

August 8, 2007 7:43 AM
 

Troy DeMonbreun said:

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!

August 20, 2007 11:35 AM
 

Troy DeMonbreun said:

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

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

Thanks!

August 20, 2007 11:46 AM
 

Shallow Dive with BDD « Convex Corners said:

Pingback from  Shallow Dive with BDD &laquo; Convex Corners

June 8, 2008 3:49 AM

Leave a Comment

(required)  
(optional)
(required)  

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