Integration Tests with 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 a browser simulator. With RSpec and Webrat you can write integration tests and test the UI of your Web App!

Assume you have a Web App with Ruby on Rails and you want to test your Landing Page. So that you ensure that some elements are for sure there. You could write a RSpec for that. Here is an example.


require 'spec_helper'

describe "landing page" do

  it "diplays the landing page" do
    get "/"
    assert_response :success
    assert_select "form[action=?]", "/search"
    assert_select "input[name=?]", "q"
    assert_select "body div.container section"
    assert_select "h1", 2
  end

end

With “get /” you are basicly sending a request to the landing page. In the next line you just expect that you get a page successful back, without any HTTP 404 or 505 erors! In the next lines you ensure that there is a form on the landing page and the action of the form shows to “/search”. Further on you ensure that the input name in the form is “q”. And you ensure that there are 2 Header with h1 on the landing page.

If something of that is missing the test / spec will fail.

You can do much more with RSpec. This was just a small example. Check out the original page: http://rspec.info/

Published by Robert Reiz

CEO @ VersionEye. Passionated software developer since 1998.

One thought on “Integration Tests with RSpec

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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: