Testing With RSpec

May 4th, 2008

I love testing in ruby because it save's me time and helps me understand the code better (see Learn Ruby by testing). It gives me peace of mind about code regression and allows me to make major changes to my code with confidence. I also end up with documentation that can be used for remembering programming decisions made and processes taken.

I use rspec a Behaviour Driven Development framework for Ruby. It is a lot more intuitive to use than Test::Unit and from what I've read it seems more powerful and aims to displace Test::Unit.

Getting Started

A very good intoduction to rspec is RSpec on Rails

I started with the Official Installation Notes. I learnt how to use rspec by reading a good article called An introduction to RSpec, by David Chelimsky.

Also these articles were very helpful

1
2
3
4
# Install rspec and rspec_on_rails plugins
cd $project
ruby script/plugin install <a href="http://rspec.rubyforge.org/svn/tags/CURRENT/rspec">http://rspec.rubyforge.org/svn/tags/CURRENT/rspec</a>
ruby script/plugin install <a href="http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails">http://rspec.rubyforge.org/svn/tags/CURRENT/rspec_on_rails</a></p>
1
2
3
# Once the plugin is installed, you must bootstrap your Rails app with RSpec.
cd $project
ruby script/generate rspec

RSpec - Testing the behavour of an obejct

To test the behaviour of an object you create a file with the naming convention <name_of_object_to_test>_spec.rb

If you are testing a rails object, the spec file require's $project/spec/spec_helper.rb which allows the object to be tested in the relevant rails environment.


require File.dirname(__FILE__) + '/../spec_helper'

This will be automatically added to your spec file's if you auto generate them You can auto generate rspec files when you generate your models and controllers by prefixing 'rspec_' in front of model, controller, or scaffold when you use script/generate. ( script/generate will behave as normal with the exception of generating some rspec files and place them in $project/spec ).


script/generate rspec_scaffold blog title:string description:text

Run your spec

Then once you've written you tests. ( see vendor\plugins\rspec_on_rails\spec\rails\example ). You can run it from the command line with

1
2
3
4
cd $project
# Test the behaviour of file app/models/credit_card.rb
# -fs makes the output be displayed in format called specdoc
script/spec -fs spec/models/credit_card_spec.rb

Autotest

Is a must have for BDD testing. It automatically runs your tests when ever you change your code. This saves you typing on the command line, reduces the chance of code regression, and is quicker because it doesn't run tests on some of the files where you have not made any changes.

To use it you need to install the ZenTest gem


gem install ZenTest
Then open up a command prompt and run autotest
1
2
cd $project
autotest

You can speed autotest up a little by limiting scope of autotest

BDD Views

I found this article, Behaviour Driven Development of Rails Views,very helpful on deciding wether to be strict about using BDD on views.

I decided to only test things I was worried about i.e. can non admin users see my links, is the no products partial shown when no products and hidden when there are products. Or I'll use it if I've done a major change and want to check it was implemented on all the right pages.

Story Runner - Testing Object Intergration

Rspec comes with story runner, which allows you to do integration and acceptance testing. To get you started see Pat Maddox's screencast RSpec Story Runner and his blog article User stories with RSpec's Story Runner.

more references

Leave a Reply