Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
NUnit, Class Assertions or Constraint Assertions

I finally got around to upgrading to the most recent version of NUnit recently (Yea I know, bout time) and found out that there is a new way of doing assertions.  The new model is referred to as the 'constraint' model and follows a more fluent style interface.  Below are some examples in syntax between the 2 models.

Classic Model examples

Assert.IsNull( objectHere );
Assert.AreEqual( value1, value2 ):
Assert.IsTrue( boolValue );

Constraint Model examples

Assert.That( objectHere, Is.Not.Null );
Assert.That( value1, Is.EqualTo( value2 ) );
Assert.That( value1, Is.True );

My initial reaction to the new style of assertions was that I did not like it.  I felt that Assert.IsNull() was cleaner than Assert.That (x, Is.Null ).  However, after I read up on the documentation for the new model ( here ), I started to like it more and more.  I have always been a fan of fluent interface style syntax because to me it reads more like English (insert your language of preference here).  Also, if you read the documentation, with the latest release (post 2.4 really) the classic model now just hides the constraint model, so why not just use the constraint model to begin with.

Anyway, I was kind of excited to see the model and the possibilities it brings, but also a little 'put off' because now I have to learn something new....:(

Till next time,

Posted 01-09-2008 6:44 AM by Derik Whittaker



Sergio Pereira wrote re: NUnit, Class Assertions or Constraint Assertions
on 01-09-2008 9:21 AM

I'm admittedly not a big fan of fluent interfaces in C#. It sure does have more English word than a traditional interface but I don't think it reads all that well with all that required punctuation  of the programming language.

Maybe this is just an attempt to replicate what works well in other programming languages, I don't know. What I know is that it doesn't look like an English sentence and it looks like messy C#.

Well, I'm just cranky anyway.

Andy wrote re: NUnit, Class Assertions or Constraint Assertions
on 01-09-2008 11:55 AM

I'm actually a fan of fluent interfaces in general, but I have to say I prefer the old style of Assert when unit testing. I can't say the fluent interface adds a great deal to nunit - although maybe I'm too ingrained using the old method.

However, for mocking, fluent interfaces are superb - maybe it's because there's more method calls and so chaining them in a fluent manner just makes more sense.

Ah, I'm sure the nunit fluent interface thing will grow on me.

Eber Irigoyen wrote re: NUnit, Class Assertions or Constraint Assertions
on 01-09-2008 4:37 PM

it's a matter of preferences and/or styles

I prefer the KISS principle and the less code is better code (as long as is readable)

like Andy says, I don't think the fluent interface adds any value here

Pages tagged "fluent" wrote Pages tagged "fluent"
on 01-09-2008 10:11 PM

Pingback from  Pages tagged "fluent"

Ramanjaneya sharma wrote re: NUnit, Class Assertions or Constraint Assertions
on 01-08-2009 4:16 AM

I think that the constraint model is much more flexible and understandable than the classic model for writing assertions in NUnit..It is more structured..

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)