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.
Timeline
- Advanced Solr Filters with Phonetics
- Advanced db_free_solr
- "Fixing" acts_as_solr
- USPS Package Tracking
- Setting up rmagick on Ubuntu
- Spec refactoring
- replaceaface.com beta ultra version 0.113 build 18.2
- Executing Shell Commands in Ruby
- Tutorial - Red5, AS3, FC4, and Shared Fridge Magnets!
- Setting up a Flash Media Server on EC2 (CentOS)
- AWS/S3 may cause rake task failed
Comments
Bir Pirikette Sen Koy! Web Site Down http://www.piriketseverler.tr.gg