Follow @RoyOsherove on Twitter

Write clearer more powerful tests with the unofficial NMock 2 Drop

[If you know what a Mock Object and NMock is - feel free to skip the following paragraph]
NMock is a .NET library (framework actually) to help write unit tests for objects that have to interact with other objects (Like Class "MailSender" interacts with class "MailServer", for example). It allows you to "mimic" one or more objects during the test so that you could check various things that would be hard to check under normal circumstances (For example - instead of checking that an actual mail message was sent ton an exchange server, you would just check that a correct call with correct parameter values was executed against the server class. You do this by "mimicking" the server class and replacing it with your own - using this NMock library).
In short, the library allows you to create what is known as a "Mock Object". If you want to get deep into what this really means, read Fowler's opinion on this. Here's a shorter, more technical concise idea (exec summary type).
Anyway,
Looks like there's a new NMock in town (though it's still in "source" mode and not yet official).
It's the NMock you wanted but your daddy didn't let you. And It's called NMock2.
What does it "Do"? well, it does some stuff the NMock you know and love today can't.
  • Finally able to verify that Events have occurred on the class under test (Much like my feeble attempt here)
  • Has an all new way of syntax - much more like it's original padre - the JMock framework from Java. Here's a small example:
    • Expect.Once.On(joker).Method("Respond").With(Is.EqualTo("Who's there?"));

      .Once.On(joker).Method("Respond").With(Is.EqualTo("Who's there?"));
    • Expect.AtLeastOnce.On(joker).Method("Ha");

      .AtLeastOnce.On(joker).Method("Ha");
  • Still cannot Mock actual classes (which is when you bring in heavy duty stuff like TypeMock.net)

  • Lots more I still haven't checked out.

So, if you're into "real" testing where you need Mock Objects, this should be worth checking out. How? There are two ways:
The Loooong Way
  1. Read the full instructions here
  2. Get some sort of CVS manager (Like Sourcesafe - only very much not!)
  3. Use it to get the latest version from the site
  4. Build the things
  5. Use it
The Short Way
  1. I already did that so you don't have to. Download the unofficial drop here: http://files.osherove.com/NMock2_Drop.zip.
  2. It includes source and binaries.
A great way to see how you should use it (including call syntax) is to read it's acceptance and unit tests. They're right there along with the code so they make for perfect API documentation for the uninitiated. Just another great example of unit tests doubling as documentation (though I agree that that's not all the documentation a project should have).
Have fun!
(link found via TestDriven.com)

Use Mock Logs instead of expectations when it makes sense

Flickr! - get ready for Flippr! (and a new Israeli dev blogger)