Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using both webrat and capybara together

I've been using Capybara for integration/request testing, but have only just realised I can't do view testing with it.

This SO answer suggests Webrat and Capybara can be used in tandem; but the RSpec docs suggest one must choose between the two. Here's another github thread that suggests webrat can be used for views and capybara for integration.

I've found that if I include Webrat in my gemfile, I can use webrat for views with no problem, but my capybara-styled integration tests no longer work. Specifically, I get an error with the following simple example:

it "should have a Home page at '/'" do
  visit '/'
  page.should have_selector('title', :content => "Home page")
end

I get the error:

No response yet. Request a page first.

What's the best way (if any?) to get webrat and capybara to like eachother?

like image 679
Rob d'Apice Avatar asked May 17 '11 04:05

Rob d'Apice


2 Answers

There's generally no reason to use both Webrat and Capybara. Pick one (probably Capybara). View tests are a bad idea and shouldn't be necessary in general; usually your integration tests should cover that ground.

In other words, fix your testing strategy and the problem will go away.

like image 128
Marnen Laibow-Koser Avatar answered Nov 15 '22 21:11

Marnen Laibow-Koser


In general, I agree with Marnen about "just pick one of them, probably Capybara", but one possible reason to use both of them is gradual migration.

Say, you have a large test suite and you're migrating it to Capybara, but you'd like to let some of your old tests to stay "Webrat-driven" for some time.

Although, I didn't find ideal solution for this case, here's what I did:

# features/support/env.rb
...
if ENV['WITH_WEBRAT'].nil?
    require 'capybara/rails'
    require 'capybara/cucumber'
    ...
else
    require 'webrat'
    ...
end
...

# config/cucumber.yml
...
default: --profile capybara
capybara: <% std_opts %> --tags ~@webrat features
webrat:   <% std_opts %> --tags @webrat features WITH_WEBRAT=1
...

# features/webrat.feature
@webrat
...

# features/capybara.feature
...

Now, you can do cucumber to run your capybara-only test suite or cucumber -p webrat for your legacy Webrat features.

Not ideal, but it worked for me.

like image 22
Alexis Avatar answered Nov 15 '22 19:11

Alexis