I'm starting to use Rspec, but when I run bundle exec rspec I get an error
/spec/requests/pages_spec.rb:20:in `block (2 levels) in <top (required)>': undefined local
variable or method `root_path' for #<Class:0x00000102e46850> (NameError)
I do not have Spork or Guard running so the question below shouldn't apply
undefined local variable or method `root_path' (Rspec Spork Guard)
I have added config.include Rails.application.routes.url_helpers in my spec_helper.rb file, but that did not help.
undefined local variable or method `root_path' Hartl's Tutorial Chapter 5.3.2
Here's pages_spec.rb
require 'spec_helper'
describe "Pages" do
describe "navigation" do
def self.it_should_be_on(path_name, value=nil)
before { visit path_name }
it "should be on #{path_name}" do
page.should have_link('Home')
page.should have_link('Inventory')
page.should have_link('FAQ')
page.should have_link('About Us')
page.should have_link('Location')
page.should have_link('Contact Us')
# page.should have_link('Login')
end
end
it_should_be_on root_path
it_should_be_on faq_path
it_should_be_on about_path
it_should_be_on location_path
it_should_be_on contact_path
# it_should_be_on login_path
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
config.include Rails.application.routes.url_helpers
end
Update
After reading about shared_examples, I tried this out successfully. Is there a better way to write this test? I ended up separating out the pages into individual pages like Home page etc.
require 'spec_helper'
describe "Pages" do
subject { page }
shared_examples "navigation" do |path_name|
before { visit send( path_name) }
describe "navigation links should be on #{path_name}" do
it { should have_link('Home') }
it { should have_link('Inventory') }
it { should have_link('FAQ') }
it { should have_link('About Us') }
it { should have_link('Location') }
it { should have_link('Contact Us') }
# it { should have_link('Login') }
end
end
describe "Home Page" do
include_examples "navigation", :root_path
end
end
None of the Rails helpers are available at the top level of RSpec's describe block. They are only available within the lower level blocks (e.g. let, before, it, etc.).
If you want to share code such as this across examples, you can use a shared_context or a shared_example, as described in the RSpec documentation, or switch to using symbols as parameters while at the describe level and defer interpretation of them as methods until you're within the lower level blocks, as shown in the answer from #IharDrozdov.
To save your structure - you can change the code like this:
require 'spec_helper'
describe "Pages" do
describe "navigation" do
shared_examples_for 'main page' do |path_name|
before { visit send(path_name) }
it "should be on #{path_name}" do
page.should have_link('Home')
page.should have_link('Inventory')
page.should have_link('FAQ')
page.should have_link('About Us')
page.should have_link('Location')
page.should have_link('Contact Us')
# page.should have_link('Login')
end
end
it_should_behave_like 'main_page', :root_path
it_should_behave_like 'main_page', :faq_path
it_should_behave_like 'main_page', :about_path
it_should_behave_like 'main_page', :location_path
it_should_behave_like 'main_page', :contact_path
# it_should_behave_like 'main_page', :login_path
end
end
because "paths are not defined at the class level in specs"(c)
You cannot call path methods in spec class. It should be in it block.
And your structure is not perfect. It will be better to put code in modules and then include it, if you want to avoid the duplication.
Related
some_spec.rb
require 'spec_helper'
describe "Authentication." do
subject { page }
let(:user) { FactoryGirl.create(:user) }
before {visit new_user_session_path}
describe "Login with username" do
before do
fill_in "Login", with: user.username
fill_in "Password", with: user.password
click_button "Sign in"
end
expect(page).to have_content('Signed in successfully.')
end
describe "Login with email" do
before do
fill_in "Login", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
expect(page).to have_content('Signed in successfully.')
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'
require 'capybara/rails'
# 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.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
# ## 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
config.include Rails.application.routes.url_helpers
end
What's the problem? After run command terminal returned error: undefined local variable or method 'page'.
The following question did not help me:
rspec+capybara undefined local variable or method
I think you should remove subject { page } from your spec.
page is defined in Capybara.
Important: wrap your expectations by it blocks!
It's definitely best to divide specs up so you have specs pertaining to each aspect of the MVC architecture, but I think there is a slight crossover with controller specs and view specs.
With view specs, you should only be concerned with the view, but with controller specs I still think it would be a good idea to test that the correct view is rendered, and maybe even test the content of the view, although more in-depth testing of the content should take place in the view spec.
Despite this clear article, https://www.relishapp.com/rspec/rspec-rails/v/2-1/docs/controller-specs/render-views, describing how to do this, I just cannot integrate my view and controller specs.
I keep getting the error undefined method 'contain'!
Here's my spec_helper:
# 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 'capybara/rspec'
require 'capybara/rails'
require 'factory_girl_rails'
require 'ap'
def set(factory)
#user = FactoryGirl.create(factory)
end
def sign_up(first_name, last_name, profile_name, email, password)
visit "/"
click_link "Register"
fill_in('First name', with: first_name)
fill_in('Last name', with: last_name)
fill_in('Profile name', with: profile_name)
fill_in('Email', with: email)
fill_in('Password', with: password)
fill_in('Password confirmation', with: password)
click_button 'Sign up'
end
def sign_in(email, password)
visit "/"
click_link "Sign In"
fill_in('Email', with: email)
fill_in('Password', with: password)
click_button 'Sign in'
end
def sign_out
visit "/"
click_link "Sign Out"
end
#Webrat.configure do |config|
# config.mode = :rails
#end
#webrat
require 'capybara/poltergeist'
# Capybara.javascript_driver = :poltergeist
Capybara.javascript_driver = :selenium
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|
# true means 'yes, filter these specs'
config.filter_run_excluding stress: true
# config.current_driver = :webkit
# config.use_transactional_fixtures = false
# config.include Capybara::DSL
DatabaseCleaner.strategy = :truncation
config.after(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
# config.before(:suite) do
# DatabaseCleaner.strategy = :transaction
# DatabaseCleaner.clean_with(:truncation)
# DatabaseCleaner.start
# end
# config.after(:each) do
# DatabaseCleaner.clean
# end
#config.after(:suite) do
# DatabaseCleaner.strategy = :transaction
# DatabaseCleaner.clean_with(:truncation)
# DatabaseCleaner.clean
# end
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
# config.include RSpec::Rails::RequestExampleGroup, type: :feature
# 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
I18n.enforce_available_locales = 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"
end
Here's my controller spec:
require "spec_helper"
describe UserFriendshipsController, type: :controller do
render_views
let (:user_1) { FactoryGirl.create(:user_1)}
before {
sign_in user_1
get :index
}
it "renders the :index view" do
response.should render_template(:index)
end
it "view contains expected html" do
# a sanity test more than anything
response.should contain("Welcome to the home page")
end
end
Upon running this spec I get this:
.F
Failures:
1) UserFriendshipsController view contains expected html
Failure/Error: response.should contain("Listing widgets")
NoMethodError:
undefined method `contain' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000008632268>
# ./spec/controllers/user_friendships_spec.rb:18:in `block (2 levels) in <top (required)>'
Finished in 0.1835 seconds
2 examples, 1 failure
Why is this happening? How can I get this to work?
If you look at the relish documentation for the current 2.14 version of Rspec you'll see that they're using match now instead:
expect(response.body).to match /Listing widgets/m
Using the should syntax, this should work:
response.body.should match(/Welcome to the home page/)
Right, it was a very unclear article from Rspec that caused this error. It was using webrat in its example and didn't think to tell you. If anyone else gets here, you can add webrat to your gemfile to use the contain method:
Gemfile
group :test do
gem 'webrat'
end
However, it makes a lot more sense to use rspec's native match method:
expect(response.body).to match /Listing widgets/m
I'm using rspec, capybara and launchy to test my web application.
Here's my spec:
require 'spec_helper'
describe "Routes" do
describe "GET requests" do
it "GET /root_path" do
visit root_path
page.should have_content("All of our statuses")
click_link "Post a New Status"
page.should have_content("New status")
fill_in "status_name", with: "Jimmy balooney"
fill_in "status_content", with: "Oh my god I am going insaaaaaaaaane!!!"
click_button "Create Status"
page.should have_content("Status was successfully created.")
click_link "Statuses"
page.should have_content("All of our statuses")
page.should have_content("Jimmy balooney")
page.should have_content("Oh my god I am going insaaaaaaaaane!!! ")
save_and_open_page
end
end
end
My .rspec
--color
--order default
and my 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'
require 'capybara/rspec'
# 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
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
DatabaseCleaner.clean
end
config.after(:each) do
DatabaseCleaner.clean
end
# 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"
end
If you look back at my spec, you'll see a rspec spec that uses capybara to browse my application, and finishes by calling the launchy gem's save_and_open_page method to open this final page in a browser for a human to look at. At this final page, however, there is no javascript or css displayed, just pure HTML.
Does anyone have any ideas why this would be? I want to test javascript, and would prefer it if all assets were loaded.
Inside config.before(:suite) do
add:
%x[bundle exec rake assets:precompile]
to precompile your Rails assets then in your test.rb environment file add:
config.action_controller.asset_host = "file://#{::Rails.root}/public"
config.assets.prefix = 'assets_test'
to point to the location of the precompiled assets. Now you can use assets when you run Capybara. Note: make sure if you are using git to ignore that new folder.
You can just add to test.rb:
config.assets.compile = true
I'm having an inexplicably hard time getting my named routes to work within my rspec tests.
Here's what I'm seeing:
1) Home GET / works!
Failure/Error: get root_path
NameError:
undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0c1538>
# ./spec/requests/home_spec.rb:6:in `block (3 levels) in <top (required)>'
2) Home GET / shows products!
Failure/Error: get products_path
NameError:
undefined local variable or method `products_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fe13b0cdea0>
# ./spec/requests/home_spec.rb:10:in `block (3 levels) in <top (required)>'
spec/requests/home_spec.rb
require 'spec_helper'
describe "Home" do
describe "GET /" do
it "works!" do
get root_path
response.status.should be(200)
end
it "shows products!" do
get products_path
response.status.should be(200)
end
end
end
spec/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}
require 'spree/core/testing_support/factories'
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.before(:suite) do
# DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
# DatabaseCleaner.start
end
config.after(:each) do
# DatabaseCleaner.clean
end
config.include Rails.application.routes.url_helpers
end
You can see that the test is including spec_helper, and spec_helper is including the routing methods. Here's my rake routes:
bundle exec rake routes
...
root / spree/home#index
products GET /products(.:format) spree/products#index
...
Any idea what could be going on here to prevent the routes from working? Thanks!
If you're wanting to reference the route of an engine from tests within an dummy application, or just an application itself, then you'll need to prefix the routing helpers with the engine name. For Spree, you would reference the routes like:
spree.products_path
Similarly, if you want to reference routes of the main application from an engine, you'll need to use main_app:
main_app.root_path
I'm working through Michael Hartl's Rails tutorial and can't get one Rspec test to pass when refactoring with a matcher.
terminal output
Failures:
1) Authentication login with invalid information
Failure/Error: it { should have_error_message('Invalid') }
NoMethodError:
undefined method `has_error_message?' for #<Capybara::Session>
# ./spec/requests/authentication_pages_spec.rb:21:in `block (4 levels) in <top (required)>'
spec/support/utilities.rb
RSpec::Matchers.define :have_error_message do |m|
match do |page|
page.should have_selector('div.alert.alert-error', text: m)
end
end
spec/requests/authentication_pages_spec.rb
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "login page" do
before { visit login_path }
it { should have_selector('h1', text: 'Login') }
it { should have_selector('title', text: 'Login') }
end
describe "login" do
before { visit login_path }
describe "with invalid information" do
before { click_button "Login" }
it { should have_selector('title', text: 'Login') }
it { should have_error_message('Invalid') }
end
describe "after visiting after page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { valid_login(user) }
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Logout', href: logout_path) }
it { should_not have_link('Login', href: login_path) }
describe "followed by logout" do
before { click_link "Logout" }
it { should have_link('Login') }
end
end
end
end
Why is it complaining about a has_error_message? method that's not defined anywhere?
Edited/added:
spec/spec_helper.rb
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
#require 'spork/ext/ruby-debug'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
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}
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
config.mock_with :rspec
# 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
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
Are you require'ing your spec/support/utilities.rb file? You may need something like this in your spec/spec_helper.rb file to actually load the file where you define the custom matcher:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Update: If you're using Spork, your utilities.rb file may be getting cached by the Spork server, meaning you would need to restart your master Spork process before the new matcher could be used by RSpec (see details on the Spork wiki).
Some people use this trick to force Spork to always reload support files:
Spork.each_run do
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
end
If you're using a later version of Capybara or RSpec (ie not the ones explicitly specified in the tutorial), if I recall correctly the way that those custom matchers needed to be written changed (though I could be wrong about that) Nevertheless, try changing your method from
RSpec::Matchers.define :have_error_message do |m|
match do |page|
page.should have_selector('div.alert.alert-error', text: m)
end
end
to:
RSpec::Matchers.define :have_error_message do |m|
match do |page|
page.has_selector?('div.alert.alert-error', text: m)
end
end
This will then hopefully enable you to write tests like:
it { should have_error_message("Invalid") }
it { should_not have_error_message("Invalid") }
I had a similar issue that I outlined in this StackOverflow Q&A.