Using both webrat and capybara together
Asked Answered
A

2

10

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?

Avoidance answered 17/5, 2011 at 4:13 Comment(5)
I'm having the same problem. I must say though, a month ago I was using the together on a different project no problem. Doesn't seem to work anymore. One other thing, I tried updating capybara to 1.0.0.beta1. I just get different error messages though.Interment
I've given up and just started using capybara without webrat. I now test my views in a roundabout way using capybara's integration testing methods - definitely not ideal but will do. I'm sure there is a way to get them working together though, let's hope someone comes a long with a good answer!Doubleedged
Quick question, how are you testing put and delete actions with capybara in controllers? It seems to only have a 'visit' action which can only issue GET requests?Interment
@duckyfuzz: Try something like visit whatever_path, :put, {'key' => 'value'} and visit whatever_path, :deleteFootstall
@Tyler: I haven't had a chance to test your suggestion yet but I have an open question on this issue from a while ago so maybe you might like to post your suggestion there and if I can get it to work I'll accept your answer? #5908716Interment
L
11

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.

Lavish answered 6/6, 2011 at 14:46 Comment(1)
Also, try to avoid using RSpec for user-facing testing. Cucumber is much better at this.Lavish
D
6

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.

Digression answered 25/11, 2011 at 17:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.