Migrating from Webrat to Capybara...unsuccessfully - ruby-on-rails

Hoping someone might see what I've overlooked...
I'm trying to get Capybara working in a small existing application...and I'm not having any luck.
Gemfile:
group :development, :test do
gem 'rspec-rails'
# gem 'webrat'
gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
end
...
Similar specs in two places are failing for different reasons. Not sure why?
spec/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:
(may be pulling in some generic page as doctype in 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)>'
spec/views/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)>'

Just had the same issue and found that capybara needs:
response.body.should have_selector("title")
webrat does not need the .body
also, make sure you are rendering views eg:.
describe PagesController do
render_views

You should include capybara DSL in your test file :
require 'spec_helper'
describe PagesController do
include Capybara::DSL
describe "GET 'about'" do
it "should be successful" do
...

This won't fix all your problems, but Instead of:
page.html.should match(/About/i)
try:
page.should match(/About/i)
(you might not even need page).
Also, check that you have Capybara set to use CSS queries in env.rb (it defaults to XPath).

Ensure that your spec_helper.rb file has the following at the top:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
I had a similar issue as yours (i.e., undefined method 'visit') and popping those lines solved the issue.

Related

Test layout in rails 4 with Rspec 3

I put a bootstrap navbar in my layout and want to test whether all the links are correct.
require 'rails_helper'
describe "layouts/application.html.erb" do
it "should have the right links in navbar" do
render
expect(page).to have_link('Home', href: "/home/index")
expect(page).to have_link('Games', href: "/games/index")
expect(page).to have_link('Programming', href: "/programming/index")
expect(page).to have_link('Bananenmannfrau', href: "/pages/about")
end
end
This just returns error:
layouts/application.html.erb should have the right links in navbar
Failure/Error: render
NameError:
undefined local variable or method `render' for #<RSpec::ExampleGroups::LayoutsApplicationHtmlErb:0x00000005a8f750>
# ./spec/layouts/application.html.erb_spec.rb:8:in `block (2 levels) in <top (required)>'
Is this even the correct way of testing it and when yes what am I missing?
You should use capybara gem for testing front end!
Capybara
Then you will be able to use it like:
require 'rails_helper'
describe "layouts/application.html.erb" do
it "should have the right links in navbar" do
visit root_path
within(".nav-bar-selector") do
expect(page).to have_content('Home')
expect(page).to have_content('Games')
expect(page).to have_content('Programming')
expect(page).to have_content('Bananenmannfrau')
end
end
end

Capybara NoMethodError

I am currently reading Michael Hartl's Rails book, and am on chapter 3 where you begin to write tests. My problem is, I can't get the first test to work. From googling and reading several threads no stackoverflow, as well as the Capybara readme, I have made some changes from his code however I still cannot get the visit function to work. The relevant files are included below.
spec/spec_helper.rb
require 'capybara'
require 'capybara/rspec'
Rspec.configure do |config|
config.include Capybara::DSL
end
Capybara.configure do |config|
config.app = "Sample App"
end
features/static_pages_spec.rb
require 'spec_helper'
feature "Static pages" do
feature "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
And the relevant error message that is throwing me for a loop:
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `call' for "Sample App":String
#./spec/features/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Seeing the error I have tried removing config.app from spec_helper.rb, but that just leads to another error.
Failure/Error: visit '/static_pages/home'
ArgumentError:
rack-test requires a rack applicaiton, but none was given
#./spec/features/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Not sure what the reasoning is, but after doing more research to fix this issue in spec/features/static_pages_spec.rb I needed to change require 'spec_helper' to require 'rails_helper'.

`Zlib::GzipFile::Error` when using `vcr` with rspec

I'm doing Railscast #291 Testing with VCR (Pro).
I want to use rspec with vcr. The tests without vcr pass with this code.
# spec/requests/zip_code_lookup_spec.rb
require "spec_helper"
describe "ZipCodeLookup" do
it "show Beverly Hills given 90210" do
visit root_path
fill_in "zip_code", with: "90210"
click_on "Lookup"
page.should have_content("Beverly Hills")
end
end
And as the tutorial I've put code in a VCR.use_cassette like this:
require "spec_helper"
describe "ZipCodeLookup" do
it "show Beverly Hills given 90210" do
VCR.use_cassette "zip_code/90210" do
visit root_path
fill_in "zip_code", with: "90210"
click_on "Lookup"
page.should have_content("Beverly Hills")
end
end
end
And created this file:
# spec/support/vcr.rb
VCR.configure do |c|
c.cassette_library_dir = Rails.root.join("spec", "vcr")
c.stub_with :fakeweb
end
According to the tutorial the rspec test should pass with this, but it fails with this error:
1) ZipCodeLookup show Beverly Hills given 90210
Failure/Error: click_on "Lookup"
Zlib::GzipFile::Error:
not in gzip format
# ./app/models/zip_code.rb:6:in `initialize'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `new'
# ./app/controllers/zip_code_lookup_controller.rb:3:in `index'
# ./spec/requests/zip_code_lookup_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/requests/zip_code_lookup_spec.rb:5:in `block (2 levels) in <top (required)>'
I have no idea why gzip appears here and not in the rest of the rails project.
How can I solve this problem?
Just had the same issue: the fakeweb gem is now deprecated in VCR, use webmock instead
in your Gemfile, replace
gem 'fakeweb'
by
gem 'webmock'
re-bundle, and you should be sorted

Ruby on Rails TDD; Error while testing

I am trying to follow TDD on Rails Tutorial which is available online here
While testing first app, I got an error.
My spec.rb code is this:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
After running testing I got this error:
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for # <RSpec::Core::ExampleGroup::Nested_1::Nested_1:0xa833e5c># ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
I will really appreciate your help.
The visit method is not part of RSpec - it's provided by capybara. Just add this to your Gemfile:
gem 'capybara'
Try add:
require 'rails_helper'
require 'spec_helper'
to your spec.rb
and:
require 'capybara'
RSpec.configure do |config|
config.include Capybara::DSL
....
to spec_helper.rb
and gem 'capybara', '2.2.0' to gemfile

Broken controller tests after installing Capybara?

I had a bunch of combined controller/view tests written with rspec. I added the Capybara gem and wrote some integrations tests which pass fine. The only problem is that now in all my controller tests, where I have
response.should have_selector("some selector")
rspec gives errors such as:
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa03e7ec>
when I run controller tests. I'm guessing that Capybara is being used in my controller tests and has overwritten some Rspec methods. How can I fix this?
# gemfile.rb
group :test do
gem 'rspec'
gem "capybara"
gem "launchy"
gem 'factory_girl_rails', '1.0'
end
# spec_helper.rb
RSpec.configure do |config|
config.include IntegrationSpecHelper, :type => :request
end
Here's an example of a failing test:
# spec/controllers/books_controller_spec.rb
require 'spec_helper'
describe BooksController do
render_views
it "should have the right page title" do
get :show, :id => #book.ean
response.should have_selector("title", :content => "Lexicase | " + #book.title)
end
end
and it's associated error:
1) BooksController GET 'show' should have the right page title
Failure/Error: response.should have_selector("title", :content => "Lexicase | " + #book.title)
NoMethodError:
undefined method `has_selector?' for #<ActionController::TestResponse:0xa8488c0>
# ./spec/controllers/books_controller_spec.rb:23:in `block (3 levels) in <top (required)>'
You were probably using Webrat earlier, and has_selector? is a Webrat matcher. Capybaras doesn't have a has_selector matcher, it has a matcher called has_css. You may want to replace the "has_selector" with "has_css".
Capybara helpers only works within requests specs. Either create a new request spec, or pass in :type => :request in the describe block part, like so:
describe "test for the testing test", :type => :request do
it "should work with capybara" do
visit root_path
click_link "Home"
page.should WHATEVA
end
end
I realize this question was asked a long time ago, but I thought I would share anyway. GLHF

Resources