Using RSpec with Capybara 2

Reading Time: 2 minutes

Using RSpec with Capybara 2

As you probably know by now, the new version of Capybara has been released and it brings a lot of changes, but the more important thing is the new syntax.

This new feature doesn't bring significant changes to your actual tests, it just needs a few more steps to catch up with your code.

To upgrade to Capybara 2.0, you'll need to do a few things:

  • Upgrade rspec-rails to 2.12.0 or greater
  • Move all the tests from spec/request to spec/features

Also you will need to use feature instead of describe and scenario instead of it, this to make it more readable.

Examples

# spec/requests/signup_spec.rb</italic>
describe "the signup process", :type => :feature do
  before :each do
    User.make(:email => 'user@example.com', :password => 'caplin')
  end

  it "signs me in" do
    visit '/sessions/new'
    within("#session") do
      fill_in 'Login', :with => 'user@example.com'
      fill_in 'Password', :with => 'password'
    end
    click_link 'Sign in'
    page.should have_content 'Success'
  end
end

With the new syntax/convention:

# spec/features/signup_spec.rb</italic>
feature "the signup process", :type => :feature do
  before :each do
    User.make(:email => 'user@example.com', :password => 'caplin')
  end

  scenario "signs me in" do
    visit '/sessions/new'
    within("#session") do
      fill_in 'Login', :with => 'user@example.com'
      fill_in 'Password', :with => 'password'
    end
    click_link 'Sign in'
    page.should have_content 'Success'
 end
end

As you can notice, the implementation isn't very complex, feature is in fact just an alias for describe, background is an alias for before, scenario for it, etc.

In order to do API HTTP requests with RSpec, you would do it with get, post , put, delete and assert instead of using response.

Another important change is the new way to skip scenarios, what you you used before was:

xit "signs me in" do
  # code of your test
end

and now is:

xscenario "signs me in" do
  # code of your test
end

In the official documentation you will find more interesting information about the new features/changes made for Capybara 2.

And that's it, thanks for reading!

0 Shares:
You May Also Like