1/22/2009

BDD test framework Shoulda

Behavior Driven Development is an interesting slant on testing. There appear to be many opinions on its advantages and disadvantages. At the end of the day, if you are testing, you have already won.

So, the question is, how can you tweak your method to improve on your bugs-per-line-of-code metric. This post isn't about that because I have no idea if BDD or Shoulda will help.

Shoulda is an alternative BDD framework to RSpec that has been argued about for so long. I can't help you with that either.

Instead, I just started using Shoulda in a Rails app so thought I would share some complaints, because being constructive is just too much work.

I switch a simple controller test over to Shoulda and it ran and passed but the original 3 tests that used to pass are now reported as "0 tests, 0 failures, 0 errors". While I proved that if there was an error it did explain precisely what was wrong to me, this 0 tests thing is severely demotivating. It is amazing how watching the number of tests grow in your project can encourage you to continue to strive for perfection.

IDE integration is always a problem with new stuff. NetBeans allows me to run a single test case with ctrl-shift F6 which is convenient if you have few that are suddenly failing. No such luck with the Shoulda version, all tests all the time.

However, all this is trivia compared to being able to do something like this:

require 'test/unit'
require 'shoulda'

class Lion
def speak
"rarrrr hickup"
end
end

class TestLion < Test::Unit::TestCase

context "the lion" do
setup do
@lion = Lion.new
end

should "say rarrr" do
assert_equal "rarrr", @lion.speak
end
end
end

and get this explanation of what you did wrong.

1) Failure:
test: the lion should say rarrr. (TestLion):
<"rarrrr"> expected but was <"rarrr hickup">.

Craft your feedback as carefully as you craft your output.

4 comments:

Matt Jankowski said...

Is there anything weird about your app? You SHOULD be seeing the normal tests/assertions/errors/fails counts that you with vanilla test::unit.

Dan Croak said...

Hi Ed,

Could you post your controller code that had the 0 tests thing? Maybe to the Shoulda mailing list and we'll try to help you out there?

Thanks,
Dan

Erno Mononen said...

FYI, in NetBeans 7.0 dev builds running a single Shoulda test works, though there are still a couple of cases that are not handled - you can track the issue here.

erno mononen said...

Another thing that just occurred to me is that if you see the problem wrt. no test reported only in NetBeans, you're likely facing this issue - it's fixed, but not in 6.5 - the fix will be delivered in patch 2 for 6.5 (in a week or two I think). You can also try a dev build.