1. Skip to navigation
  2. Skip to content

The ELC Community Blog

A knowledge exchange on Ruby on Rails and Agile Development


Spec refactoring

by josh on January 30, 2008

While trolling through old code, I came across the following specification:

describe Build do
  describe "passed?" do
     it "returns true for 1" do
       build = Build.new(:status => 1)
       build.passed?.should be_true
     end
  end
end

The problem with this test is it only tests for success. Realizing that it's equally--if not more--important to test for failure than it is success, I decided to ask the team for opinions on what this test should look like.

After bouncing this around with the team, we decided to refactor it as such:

describe Build
 describe "passed?" do
     before(:each) do
       @build= Build.new
     end

    it "returns false when status has not been set" do
      @build.passed?.should be_false
    end

     it "returns false when status is 0"
       @build.status= 0
       @build.passed?.should be_false
    end

     it "returns true when status is 1"
       @build.status= 1
       @build.passed?.should be_true
    end
 end
end

As you can see, we were able to find 2 failure conditions and only one success.

This is a very simple example but we think it has a broad application to testing in general.

Comments

Add a comment


home | services | Ruby on Rails Development | code | blog | company