RSpec

RSpec is a Test Framework for Ruby! And RSpec is just awesome! It is the best Test Framework I know. Add this 2 lines to your Gemfile to the test group and the development group.

gem 'rspec-rails', '2.8.0'
gem 'webrat', '0.7.3'

Webrat is browser simulator. With RSpec and Webrat you can write integration tests and test the UI of your Web App!

Assume you have a simple class like that:

class Bowling
  def hit(pins)
  end

  def score
    0
  end
end

Than your RSpec Code would look like that:

require 'bowling'

describe Bowling, "#score" do
  it "returns 0 for all gutter game" do
    bowling = Bowling.new
    20.times { bowling.hit(0) }
    bowling.score.should eq(0)
  end
end

In RSpec you don’t write tests, you write specifications. You describe a class and how it should behave! The name of a “test method” / “specification” is a regular string. I t can be a whole sentence or two!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s