As our project gets larger and larger at RideCharge, I have become more reliant on our test suite. We don’t do anything fancy, just use the de facto rails Test::Unit. I have never been serious about BDD, but I think I would like it.

I have flirted with and really like the idea of rspec, but on a project where you have a large existing test suite Shoulda is a drop in enhancement. It’s biggest win is that it allows you to give your tests more context. But, there is a secondary reason for using Shoulda, it allows you to follow the 1 test 1 assertion rule without breaking a sweat.

example:

context "a user that is in pending state" do
  setup {@user = User.find(:first, :conditions => {:state => "pending"})}
  should "not be active" do
    assert !@user.active?
  end
end

You can just put this code right in your User unit test without making any other changes. When the test fails it will read, a user that is in pending state should not be active. That is more readable, especially when the build breaks and you have to dig though a thousand assertions to figure out which one is the culprit.

Post Information

Tags:

We're Reading

Feeds/Syndication

Leave a Reply