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.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Joe Ocampo

My personal philosophy is simple: "Have a good strategy that sets the environment for success through the enablement of the whole. Be agile but with a mind towards pragmatism. Delegate to the best qualified individuals, but don’t be afraid to involve yourself in all parts of a job. Treat everyone with respect, humility, and with a genuine pursuit towards excellence." Respected business and technical leader with expertise in directing organization towards effective results driven outcomes. Proven ability to perform and communicate from both technical and business perspectives. Strong technical and business acumen developed through experience, education and training. Provides the ability to utilize technology, harness business intelligence and execute strategically by optimizing systems, tools and process. Passionate about building people, companies and software by containing cost, maximizing operational throughput and capitalize on revenue. Looks to leverage the strengths of individuals and grow the organization to their maximum potential by harnessing the power of their collective whole and deliver results. Co-Founder of LosTechies.com
This entry was posted in BDD (Behavior Driven Development). Bookmark the permalink. Follow any comments here with the RSS feed for this post.

7 Responses to NUnit’s new Constraint model

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

  2. That is worlds easier to read! Too bad I’ll have to wait a year before MS Test “invents” that feature.

  3. Joe Ocampo says:

    Jason, read my mind!

  4. Joe Ocampo says:

    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!

  5. Judah says:

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

  6. I just wrote an article that covers most of the Constraints’ syntax:

    Learn the new NUnit 2.4 Constraint-Based Assert Model

    Thanks!

  7. I just wrote an article that covers most of the Constraints’ syntax:

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

    Thanks!