I'm following the Ruby on Rails tutorial and ran into the following problem:
me#me:~/Ruby/sample_app$ 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:0x00000001e1df88>
# ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
Finished in 0.0006 seconds (files took 0.08149 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page should have the content 'Sample App'
Here is my Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
group :development, :test do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'rspec-rails'
end
group :assets do
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
end
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
group :test do
gem 'capybara'
end
group :prodcution do
gem 'pg'
end
My spec/requests/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_content('Sample App')
end
end
end
Digging up online suggested putting config.include Capybara::DSL in spec/spec_helper.rb. I did that and didn't work. Another suggestion said I need to move my static_pages_spec.rb to spec/features. I did that and that didn't work either.
Help? :D
visit is a method used in feature specs.
In your request specs you instead use the HTTP verbs get, post, patch etc to send http requests.
require 'rails_helper'
RSpec.describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
get '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
I believe you are using a badly outdated tutorial as well as subject.should has been depreciated for a long time in favor of
expect(subject).to.
While you can mix capybara into your request specs it may be better to use request specs as a low level integration tests and leave the higher level clicking and button pushing to your feature specs.
When you run into trouble you might want to refer to the official RSpec-rails documentation instead.
https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec
https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/feature-specs/feature-spec
Related
New to Rails and going through this books tutorial. Been scratching my head on this one for nearly day now so I could use some help. Unable to get this test to pass from section 3.3.4 of the 2nd Edition book. (Ruby on Rails Tutorial) See failures and code below:
Failures:
1) Static pages Home page should have the title 'Home'
Failure/Error: page.should have_selector('title', :text => 'Ruby on Rails Tutorial Sample App | Home')
expected to find css "title" with text "Ruby on Rails Tutorial Sample App | Home" but there were no matches. Also found
"", which matched the selector but not all filters.
# ./spec/features/static_pages_spec.rb:15:in `block (3 levels) in <top (required)>'
2) Static pages Help page should have the title 'Help'
Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
expected to find css "title" with text "Ruby on Rails Tutorial Sample App
| Help" but there were no matches. Also found
"", which matched the selector but not all filters.
# ./spec/features/static_pages_spec.rb:28:in `block (3 levels) in <top (required)>'
3) Static pages About page should have the title 'About Us'
Failure/Error: page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About")
expected to find css "title" with text "Ruby on Rails Tutorial Sample App
| About" but there were no matches. Also found
"", which matched the selector but not all filters.
# ./spec/features/static_pages_spec.rb:41:in `block (3 levels) in <top (required)>'
static_pages_spec.rb
require 'spec_helper'
require 'rails_helper'
describe "Static pages" do
describe "Home page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => 'Sample App')
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title',
:text => 'Ruby on Rails Tutorial Sample App | Home')
end
end
describe "Help page" do
it "should have the h1 'Help'" do
visit '/static_pages/help'
page.should have_selector('h1', :text => 'Help')
end
it "should have the title 'Help'" do
visit '/static_pages/help'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About page" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => 'About Us')
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_selector('title',
:text => "Ruby on Rails Tutorial Sample App | About")
end
end
end
application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Ruby on Rails Tutorial Sample App | <%= yield(:title) %></title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
end
home.html.erb
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
help.html.erb
<% provide(:title, 'Help') %>
<h1>Help</h1>
<p>
Get help on the Ruby on Rails Tutorial at the
Rails Tutorial help page
. To get help on this sample app, see the
Rails Tutorial book.
</p>
about.html.erb
<% provide(:title, 'About Us') %>
<h1>About Us</h1>
<p>
The Ruby on Rails Tutorial
is a project to make a book and screencasts to teach web development
with Ruby on Rails. This
is the sample application for the tutorial.
</p>
gemfile
source 'https://rubygems.org'
gem 'minitest'
gem 'test-unit'
gem 'rails_12factor'
ruby '2.1.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
gem 'sqlite3'
gem 'capybara'
gem 'rspec-rails'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :development do
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
end
group :production do
gem 'pg'
end
If anyone can get me pointed in the right direction, it would be greatly appreciated. I prefer to stick to solutions closest to the books methods but if I have to do something differently to get this to pass and proceed onwards then I'll take what I can get. Just please to don't suggest me doing anything crazy advanced that I clearly should be getting into yet if at all possible. Thanks. Simple is better here.
You're using RSpec and Capybara matchers in your spec, but you don't have those gems installed.
Here is the Gemfile from the 2nd edition:
Gemfile
source 'https://rubygems.org'
gem 'rails', '3.2.16'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
Looking at your Gemfile, it appears you're using a newer version of Rails. If you are following Michael's tutorial, you should use the same versions that he specifies. There is a newer edition of the tutorial available, suited for Rails 4 development. You can read it here for free:
https://www.railstutorial.org/book
I'm following Michael Hartl's Ruby on Rails Tutorial and I'm getting the following error in section 3.2.1 when I run:
bundle exec rspec spec/requests/static_pages_spec.rb
I've seen similar errors posted but none exactly the same. Any assistance would be appreciated.
Error:
/Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:791:in `<': compared with non class/module (TypeError)
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:791:in `safe_include'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:785:in `block in configure_group'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:783:in `each'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:783:in `configure_group'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/world.rb:47:in `configure_group'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:290:in `set_it_up'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:241:in `subclass'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:228:in `describe'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/dsl.rb:18:in `describe'
from /Users/michaeltro/RubymineProjects/sample_project/spec/requests/static_pages_spec.rb:3:in `<top (required)>'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /Users/michaeltro/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'enter code here
Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'
# Use sqlite3 as the database for Active Record
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
# Use SCSS for stylesheets
gem 'sass-rails', '4.0.1'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '2.1.1'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '4.0.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails', '3.0.4'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '1.1.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '1.0.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'
# Use unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
Routes.rb:
SampleProject::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
end
static_pages_spec.rb
require 'spec_helper'
describe "Static Pages" do
describe "Home page" do
it "should have the content 'Sample Project'" do
visit '/static_pages/home'
expect(page).to have_content('Sample Project')
end
end
end
spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara: :DSL
end
Try to put your static_pages_spec.rb file like this:
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
it "should have the base title" do
visit '/static_pages/home'
expect(page).to have_title("Ruby on Rails Tutorial Sample App")
end
it "should not have a custom page title" do
visit '/static_pages/home'
expect(page).not_to have_title('| Home')
end
end
end
I can't make Capybara to work in many of my integration tests.
When I visit certain pages, I get the following html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
And so when I try to use the have_content() selector, it raises the following error:
Failure/Error: page.should have_content("HELLO")
Capybara::ElementNotFound:
Unable to find xpath "/html"
Some of the pages of my apps I can visit just fine, but some other ones I can't. There are even some pages that work at some places, and not at others:
require 'spec_helper'
describe HomeController do
it "shows the website description" do
visit root_path
puts page.html.length # ==> 108 (no html)
...
end
end
require 'spec_helper'
describe FlowsController do
it "should do stuff" do
visit root_path
puts page.html.length # ==> 4459, works even if the exact same one didn't work in home_controller_spec!
visit flows_path
puts page.html.length # ==> 3402, works
visit new_user_session_path
puts page.html.length # ==> 3330, works
within("form#new_user") do
fill_in 'Email', :with => 'email#example.com'
fill_in 'Password', :with => 'password'
click_on 'Sign in'
end
puts page.html.length # ==> 108, No html
end
end
I read in this post that this was an error that might occur when using Capybara and webrat at the same time. But I'm not using webrat at all, and I still get the error...
Here is my Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.6'
gem 'pg'
gem 'thin'
gem 'devise'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
end
# Add rspec for testing
group :test, :development do
gem "rspec-rails", "~> 2.0"
gem "capybara"
gem "factory_girl_rails"
end
gem 'jquery-rails'
Try passing an String to describe, instead of the Controller class
describe "whatever" do
...
end
I am following the tutorial from Michael Hartl . I tried the first test example for testing the app with Rspec and when I execute this command "bundle exec rspec spec\requests\static_pages_spec.rb" I get this error.
F
Failures:
1) Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:0x39e1510 #example=nil>
# ./spec/requests/static_pages_spec.rb:4:in `block (2 levels) in <top (required)>'
Finished in 0.001 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:3 # Home page should have the content 'Sample App'
static_pages_spec.rb
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
Gemfile.rb
source 'https://rubygems.org'
gem 'rails', '3.2.1'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.10.0'
end
group :assets do
gem 'sass-rails', '3.2.4'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
You need to require 'spec_helper' in your spec source.
Your spec_helper should include both rspec/rails and capybara/rails in it.
You'll want to use get instead of visit if you want to access the response, however.
If static_pages_spec.rb has string require 'spec_helper' and you get
Failure/Error: visit '/static_pages/home'
add to spec_helper.rb string config.include Capybara::DSL
it helped me
I am following this article where I can written this code in this ruby file below and home page does have sample app, but it still says static pages home page should have content 'Sample App' when I run bundle exec rspec spec/requests/static_pages_spec.rb
spec/requests/static_pages_spec file code:
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
home.html.erb
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
Below is my gem file for this app. please advise. thanks for your help.
source 'https://rubygems.org'
gem 'rails', '3.2.1'
gem 'sqlite3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development do
gem 'rspec-rails', '2.6.0'
end
group :test do
gem 'webrat', '0.7.1'
gem 'capybara', '1.0.0.beta1'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.3'
gem 'coffee-rails', '3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer'
gem 'uglifier', '1.0.3'
end
gem 'jquery-rails', '2.0.0'
group :production do
gem 'pg', '0.12.2'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
In the output from your failed rspec test do you have the line FATAL: database "sample_app_test" does not exist many lines above the error Failed examples: rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page should have the content 'Ruby on Rails'? If so the following steps worked for me:
Open the sample_app/config.database.yml file in a text editor, and under test, change the database to the actual name of the postgres db you using, save the .yml file, and re-run the rspec test.
I think that you need to require capybara manualy in the spec_helper.rb. That's what I normally do :)
I think this is a conflict with webrat. Remove it from you gem file.
#gem 'webrat', '0.7.1'
gem 'capybara', '1.0.0.beta1'