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'.
Related
I have just started with RSpec and Capybara but got stuck on my first test.
Here's me test code located in spec/features/pages_spec.rb :
require 'rails_helper'
RSpec.describe "Pages", :type => :request do
describe "About Page" do
it "should have the content 'About Us'" do
visit '/pages/about'
page.should have_content('About Us')
end
end
end
Running the test i get the following error :
01:06:59 - INFO - Running: spec
F
Failures:
1) Pages About Page should have the content 'About Us'
Failure/Error: visit '/pages/about'
NoMethodError:
undefined method 'visit' for #<RSpec::ExampleGroups::Pages::AboutPage:0x007f975afe7380>
# ./spec/features/pages_spec.rb:6:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:44:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:43:in `block (2 levels) in <top (required)>'
Finished in 0.02569 seconds (files took 1.65 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/pages_spec.rb:5 # Pages About Page should have the content 'About Us'
I've been searching about this for about an hour and everywhere I find the solution of moving the test code from spec/requests to spec/features.
I've also seen this here : http://www.rubydoc.info/gems/rspec-rails/file/Capybara.md which most of the solution suggests and it is not recommended to use.
# not recommended!
RSpec.configure do |c|
c.include Capybara::DSL, :file_path => "spec/requests"
end
I've no idea how to proceed. Your help will be appreciated.
Either take :type => :request out of the describe block (which overrides the spec type determined from the directory location) or change it to :type => :feature.
I'm doing the rails tutorial from Michael Hartl, and I can't get the test in chapter 3 to pass.
The static_pages.rb file:
require 'spec_helper'
describe "Static pages" do
describe "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
The home.html.erb file:
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
And this is the result of the test:
An error occurred in an after hook
ArgumentError: rack-test requires a rack application, but none was given
occurred at C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/capybara-2.1.
0/lib/capybara/rack_test/driver.rb:16:in `initialize'
←[31mF←[0m
Failures:
1) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mArgumentError←[0m:
←[31mrack-test requires a rack application, but none was given←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top
(required)>'←[0m
Finished in 0.001 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home p
age should have the content 'Sample App'←[0m
My current version of Capybara is 2.1.0 but I tried changing version to 1.1.2 and bundle update, but didn't work aswell.
Also tried different syntax for the test.
page.should have_title("Sample App")
page.should have_selector('h1', :text => 'Sample App')
page.should have_selector('title', text: 'Sample App')
My system is Windows 7, and my rails version is 4.0.8 and Ruby version is 1.9.3.
Tried searching around for the "rack-test requires a rack application, but none was given" error, and it seems I somehow need to change so that rack is not the default driver.
Well this is how my config file looks like.
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'capybara/rspec'
RSpec.configure do |config|
config.include Capybara::DSL
end
I am following Michael Hartls tutorial on rspec and I am getting this error
bundle exec rspec spec/requests/static_pages_spec.rb
F
Failures:
1) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x007fdddbdf6a90>
# ./spec/requests/static_pages_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00052 seconds (files took 0.1562 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # Static pages Home page should have the content 'Sample App'
for the rspec test.
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
I don't believe that the reason I am getting this error is the test case is failing but rather this:
NoMethodError:
undefined method `visit' for #<RSpec:....
I am not sure why there is an undefined method visit
maybe you just missed:
config.include Capybara::DSL
also you could look to the same qestion here
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
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.