Migrating from Webrat to Capybara ... unsuccessfully

Hoping someone can see what I missed ...

I'm trying to get Capybara to work in a small existing application ... and I'm out of luck.

Gemfile:

group :development, :test do gem 'rspec-rails' # gem 'webrat' gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git' end ... 

Similar specifications in two places fail for various reasons. Do not know why?

specifications / controllers / pages_controller_spec.rb:

 require 'spec_helper' describe PagesController do describe "GET 'about'" do it "should be successful" do # get 'about' #worked w/ webrat # response.should be_success #worked w/ webrat visit pages_about_path # page.should have_content('About Us') page.html.should match(/About/i) end it "should have title" do # get 'about' #webrat # response.should have_selector("title", :content => "About Us") #webrat visit pages_about_path page.should have_selector("title") end end end 

Failures: (maybe pull to some kind of shared page, since the doctype in the browser is "<!DOCTYPE html>" )

  1) PagesController GET 'about' should be successful Failure/Error: page.html.should match(/About/i) expected "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n\n" to match /About/i # ./spec/controllers/pages_controller_spec.rb:13:in `block (3 levels) in <top (required)>' 2) PagesController GET 'about' should have the right title Failure/Error: page.should have_selector("title") expected css "title" to return something # ./spec/controllers/pages_controller_spec.rb:20:in `block (3 levels) in <top (required)>' 

specifications / opinions / pages / about.html.haml_spec.rb:

 require 'spec_helper' describe "pages/about.html.haml" do it "renders attributes in <p>" do # render #webrat # rendered.should match(/About/) #webrat visit pages_about_path page.should have_content("About Us") end it "should have the right heading" do # render #webrat # rendered.should have_selector("h2", :content => "About Us") #webrat visit pages_about_path page.should have_selector("h2") end end 

Failures:

  1) pages/about.html.haml renders attributes in <p> Failure/Error: visit pages_about_path NoMethodError: undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000101dc2970> # ./spec/views/pages/about.html.haml_spec.rb:8:in `block (2 levels) in <top (required)>' 2) pages/about.html.haml should have the right heading Failure/Error: visit pages_about_path NoMethodError: undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x000001034b1d98> # ./spec/views/pages/about.html.haml_spec.rb:16:in `block (2 levels) in <top (required)>' 
+7
source share
4 answers

It was just the same problem and it turned out that the capybara needs:

 response.body.should have_selector("title") 

webrat doesn't need .body

also make sure that you are viewing views, for example :.

 describe PagesController do render_views 
+14
source

You should include capybara DSL in the test file:

 require 'spec_helper' describe PagesController do include Capybara::DSL describe "GET 'about'" do it "should be successful" do ... 
+1
source

This will not fix all your problems, but instead:

 page.html.should match(/About/i) 

to try:

 page.should match(/About/i) 

(you may not even need a page).

Also, make sure Capybara is set to use CSS queries in env.rb (XPath is used by default).

0
source

Make sure your spec_helper.rb file has the following:

 ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' require 'capybara/rspec' 

I had a similar problem like yours (i.e. undefined method 'visit' ), and when these lines appeared the problem was solved.

0
source

All Articles