Test Driven Development (TDD)

by Dave on May 16th, 2010

When testing Rails applications the favoured test suite is Rspec.

For Ruby versions 1.8.7 and under, the below instructions should get you going.

$ [sudo] gem install rspec -v 1.3.0
$ [sudo] gem install rspec-rails -v 1.3.2

The test folder is now named spec. You no longer need your test folder.

To setup rspec tests for a controller named Pages:
$ script/generate rspec_controller Pages home contact

Your test file pages_controller_spec.rb will contain a test looking like the following:

describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
end

The first line is a description. The second line is also and neither have any effect on the test. The third line performs a GET request to the page. The fourth line describes the desired result. should_be_success looks for status code 200.

To run your tests:
$ spec spec/

Red, Green, Refactor

This is the process you work through when following Test Driven Development. First get the test to fail, then to pass, then refactor your code.

From → General

No comments yet

Leave a Reply

You must be logged in to post a comment.