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
Related
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 :-)
So I am trying to run my test which is suppose to pass now but I keep getting an error
1) Sign in flow successful redirects to the topics index
Failure/Error: visit('/topics')
NoMethodError:
undefined method `visit' for #
<RSpec::ExampleGroups::SignInFlow::Successful:0x007fd91e36f8e0>
# ./spec/models/sign_in_spec.rb:10:in `block (3 levels) in <top (required)>'
Here is my sign_in_spec.rb file
require 'rails_helper'
describe "Sign in flow" do
include TestFactories
describe "successful" do
it "redirects to the topics index" do
user = authenticated_user
visit root_path
end
end
end
And my rails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
require 'capybara/rspec'
I am not sure but I don't think I need to create a route for root_path do I?
The issue isn't / -- it's visit as your error shows. That method doesn't exist in this spec file. That's a Capybara method, and Capybara is for feature specs.
It looks like the problem is that you're trying to run a "features" spec in a model spec file. Capybara, which creates the domain specific language you're using, sets it to be available only in certain spec folders. If you move the spec into the spec/features folder, that may fix it.
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'.
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
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.