describe User do it "should be in any roles assigned to it" do user = User.new user.assign_role("assigned role") user.should be_in_role("assigned role") end
it "should NOT be in any roles not assigned to it" do user.should_not be_in_role("unassigned role") end end
Aside from the test name being a string, you can see the actual assertions in the format ‘variable.should be_some_value’, which is kind of more readable than it might be in C#:
Admittedly, the second example is nicer to read – the trouble with that is that you’re checking a true/false result, so the feedback you get from NUnit isn’t great:
Test ‘Access_Tests.TestAddUser’ failed: Expected: True But was: False
Fortunately, there are a few tools coming out for .net now which address this situation in a bit more of a ruby-like way. The one I’ve been using recently is Shouldly, an open source project on GitHub.
And so on. Not bad, not bad, not sure if it’s worth learning another API for though. However, the real beauty of Shouldly is what you get when an assertion fails. Instead of an NUnit-style
But was: 2
– which gives you a clue, but isn’t terribly helpful – you get:
should be 3
but was 2
See that small but important difference? The variable name is there in the failure message, which makes working out what went wrong a fair bit easier – particularly when a test fails that you haven’t seen for a while.
Even more useful is what you get with checking calls in Rhino Mocks. Instead of calling
fileMover.AssertWasCalled(fm => fm.MoveFile(“test.txt”, “processed\\test.txt”));
and getting a rather ugly and unhelpful
Rhino.Mocks.Exceptions.ExpectationViolationException : IFileMover.MoveFile(“test.txt”, “processed\test.txt”); Expected #1, Actual #0.
With Shouldly, you call
fileMover.ShouldHaveBeenCalled(fm => fm.MoveFile(“test.txt”, “processed\\test.txt”));
and end up with
0: MoveFile(“test1.txt”, “unprocessed\test1.txt”)
1: MoveFile(“test2.txt”, “unprocessed\test2.txt”)
As you can see, it’s not only much more obvious what’s happening, but you actually get a list of all of the calls that were made on the mock object, including parameters! That’s about half my unit test debugging gone right there. Sweet!
Shouldly isn’t a 1.0 yet, and still has some missing functionality, but I’d already find it hard to work without it. And it’s open source, so get yourself on GitHub, fork a branch, and see if you can make my life even easier!
TekPub is a website set up by Rob Conery and James Avery, two very respected named in the world of development, to deliver video content on many development-related subjects. Rob’s one of my development heroes, who previously worked for Microsoft on ASP.Net MVC, gave us the SubSonic data access framework, and has much good content on the web already in video and blog form.
Anyway, TekPub isn’t free, a year’s subscription costs $200 (or you can pay on a per video or monthly basis), but there are several free videos on there which are well worth watching. (I luckily won a year’s free subscription at a NxtGenUG user group meeting, which is what took me there in the first place).
There are two free series of videos, which are both useful. One is based on Mike Gunderloy’s book ‘Coder to Developer’ and basically discussed how to move from a cowboy to a professional developer, making proper use of things like source control and project organisation tools. Not much new for a seasoned developer, but useful for many currently making the journey.
The other free series is called ‘Concepts‘, which (using C#) covers a lot of the things a growing developer needs to learn – IOC Containers, Unit Testing, Interfaces, and even Behaviour Driven Design (BDD).
And if you’re willing to fork out, there are tens if not hundreds of hours of content on subjects from ASP.Net MVC to iPhone development, jQuery, Git, Ruby, and many more.
So instead of turning on the TV tonight, get over to TekPub and learn something useful instead!