Skip to navigation

Getting up-and-running with RSpec

Published February 07, 2007

After all the talk about RSpec at the Rails Aus meetups, tonight I finally got around to having a play with it.

I don’t have a great track record with high test coverage, but RSpec is considerably more enjoyable, and no doubt 1000x more readable, than the equivalent Test::Unit, which can only serve help my cause.

It’s pretty easy to add RSpec to a new or existing project too. Your spec’s happily live alongside your unit tests in the filesystem, but don’t expect any integration. If you’ve got a lot of legacy Test::Unit integration then you might also want to check out test/spec.

Here’s how I installed RSpec, and then I’ll show how to create a simple model spec and run it. Check out the official RSpec install guide for update-to-date install information. The trick is just to make sure the rails plugin matches the version of your installed gem.

  1. sudo gem install rspec -y
  2. cd my_rails_app
  3. piston import svn://rubyforge.org/var/svn/rspec/tags/REL_0_7_5_1/rspec_on_rails/vendor/plugins/rspec_on_rails vendor/plugins/rspec_on_rails
  4. ./script/generate rspec
  5. mkdir -p spec/models (there’s also other dirs you might want to create)
  6. Create your first spec, spec/models/user_spec.rb

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

context "User" do
  specify "should check universal stability"
    42.should == 42
  end
end

You can now run rake spec and hopefully your spec ran and passed!


$ rake spec
.

Finished in 0.009566 seconds

1 specification, 0 failures

Next step is to add fixtures to spec/fixtures and flesh out your model specification some more.

Now you can go add “BDD” on to the bottom of that Web2.0 developer skills list ;)

Archived comments

Comments were previously allowed on articles. Though no new comments are being accepted you can see the old comments below.

  1. topfunky

    I’ve been reluctant to commit to RSpec for projects since it still appears to be under heavy development, and things change often.

    Do you feel that it’s stable enough to use 100% for a project, or is it still for the early adopters?

  2. Tim Lucas

    I’m not really sure… I’m not involved in the RSpec community enough to say. The feeling I’ve got though is that the basics are nailed down enough to use w/o worrying about things changing. I guess it’s the same call as “do I use Rails v1.2 or edge?”

  3. Lachie

    The RSpec folks have at long last committed to a 1.0 API http://rubyforge.org/pipermail/rspec-users/2007-February/000642.html

  4. Pat Maddox

    I’ve been using RSpec since 0.6.x, and I’ve always used edge. There’s been a bit of pain, but over all it’s been worth it to me.

    It’s at a point now where you can safely use it though. I’m using it for all my Ruby coding at work, anyway.

  5. Tim Lucas

    thanks Lachie, I knew you’d know. I like custom matchers.

    Thanks Pat. As this is a new project and if 0.8 wasn’t coming out in a few weeks I’d probably move to edge…

  6. Chad Humphries

    Over at The Edge Case we have used RSpec exclusively for the last several releases (since rubyconf). We have had a few ups and downs but things are great all in all.

Thoughts

toolmantim

I’m Tim Lucas, a user experience developer currently in Sydney Australia.

I occasionally write, snap photos, present on various technical topics, tweet my going-ons, share teh codes and post tidbits to the scrapbook.

Most recently I published Simplifying ticket sales on sydneyoperahouse.com (February 16, 2010)

Work with me via Agency Rainford, or shoot an email to and say hello.

Powered by vegemite