Testing tidbits

A couple of useful nuggets came up while I was pair programming with James today.

Firstly, James had an interesting new use of the Mocha parameter matcher anything, which reminded me of Joe Walnes and his Flexible JUnit assertions with assertThat.

Normally anything is used to determine what parameters are matched by an expectation…

Product.expects(:do_stuff).with('name', anything)

But James was passing anything into a method to indicate that the value of that parameter was irrelevant in the context of the test. The kind of thing you might already do using nil

Product.do_stuff('name', anything)

I wonder if this idea could be extended by having a kind of holy hand-grenade object which “blows up” by raising an AssertionFailedError when any method is invoked on it…?

Secondly, we were running all our tests using the default rake task when we discovered a test that was hanging. The standard rake test task output wasn’t very helpful in identifying which test was stuck (just rows of dots)…

Started
.....................................................................
.....................................................................
.....................................................................
......................................................

Interrupting the process and examining the stack trace didn’t help either (the relevant stack frames had been collapsed)…

... 24 levels...
from /opt/local/lib/ruby/1.8/test/unit/autorunner.rb:216:in `run'
from /opt/local/lib/ruby/1.8/test/unit/autorunner.rb:12:in `run'
from /opt/local/lib/ruby/1.8/test/unit.rb:278
from /opt/local/lib/ruby/gems/1.8/gems/rake-0.7.3/lib/rake/...

So we set the output to verbose which outputs the name of each test as it starts. From this we could work out which was the offending test…

$ TESTOPTS="-v" rake
test_should_do_something(NaughtyTest): .
test_should_do_something_else(NaughtyTest): .
test_should_do_something_ideally_without_hanging(NaughtyTest): .