undefined method `visit` rspec/capybara - ruby-on-rails

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

Related

Can't Execute $ time rspec spec/requests/static_pages_spec.rb

I'm following the Ruby on Rails Tutorial by Example by Michael Hartl but I got stuck.
Everytime I try to run the following test I got an error:
time rspec spec/requests/static_pages_spec.rb
My error:
/home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `load': cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/rspec:23:in `load'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/rspec:23:in `<main>'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `eval'
from /home/i/.rvm/gems/ruby-2.0.0-p643#railstutorial_rails_4_0/bin/ruby_executable_hooks:15:in `<main>'
real 0m0.752s
user 0m0.699s
sys 0m0.044s
static_pages_spec.rb :
require 'spec_helper'
describe "StaticPages" do
describe "Home page" do
it "It should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
it "should have the right title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help page" do
it "It should have the content 'Help'" do
visit '/static_pages/help'
expect(page).to have_content('Help')
end
it "should have the right title" do
visit '/static_pages/help'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "It should have the content 'About Us'" do
visit '/static_pages/about'
expect(page).to have_content('About Us')
end
end
it "should have the right title" do
visit '/static_pages/about'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | About Us")
end
describe "Contact" do
it "It should have the content 'Contact'" do
visit '/static_pages/contact'
expect(page).to have_content('Contact')
end
end
it "should have the right title" do
visit '/static_pages/contact'
expect(page).to have_title("Ruby on Rails Tutorial Sample App | Contact")
end
end
Gemfile: ## http://pastebin.com/1RhbaCiK
OS: Ubuntu 14.04
I've been searching for solution, but have not found one. Please keep in mind that I'm new to Ruby on Rails and web development.
Your error is saying:
cannot load such file -- /home/i/rails_projects/sample_app/spec/spec/requests/static_pages_spec.rb (LoadError)
which means RSpec can't find the spec. I see you have spec twice in the file path: ../sample_app/spec/spec/...
You should keep your specs under the spec directory of your Rails app. So, the path should be: /sample_app/spec/ like this. Fix that and try again. It should work.
Update
So, after running your code locally, I found the issue. There was a gem dependency issue with gem guard-spork.
You just need to upgrade this gem. In your Gemfile:
Change gem 'guard-spork', '1.5.0' to gem 'guard-spork', '2.1.0'
Then, bundle install
And, then run time rspec
Everything will run smoothly :-)

Capybara: undefined method 'visit' - Test is already in spec/features

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.

Can't get rspec test from railstutorial.org chapter 3 to pass

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

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'.

rspec: undefined method 'has_conent?'

Hello I am having an issue with railstutorial.
I have the test file features/static_pages_spec.rb
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_conent('Sample App')
end
end
end
when I run bundle exec rspec spec/features/static_pages_spec.rb
I get the following error:
Failures:
1) Static pages Home page Should have the content 'Sample App'
Failure/Error: page.should have_conent('Sample App')
NoMethodError:
undefined method `has_conent?' for #<Capybara::Session>
# ./spec/features/static_pages_spec.rb:9:in `block (3 levels) in <top (required)>'
Finished in 0.05262 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/static_pages_spec.rb:6 # Static pages Home page Should have the content 'Sample App'
Randomized with seed 49777
I tried to add in the spec_helper.rb config.include Capybara::DSL but it gives me the same error.
Just a typo:
page.should have_conent('Sample App')
should be
page.should have_content('Sample App')
There is a typo. You are missing a t in your content
page.should have_content('Sample App')

Resources