I'm getting the following error when I try to run an rspec test:
/srv/offerme/spec/requests/static_pages_spec.rb:13: undefined method `visit' for #<Class:0xb3436684> (NoMethodError)
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
from /srv/offerme/spec/requests/static_pages_spec.rb:12
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `subclass'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:224:in `describe'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/dsl.rb:18:in `describe'
from /srv/offerme/spec/requests/static_pages_spec.rb:3
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
from /var/lib/gems/1.8/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `autorun'
from /usr/local/bin/rspec:19
The file I get this error in looks like this (spec/requests/static_pages_spec.rb) :
require 'spec_helper'
describe "StaticPages" do
include Capybara::DSL
describe "GET /static_pages" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get static_pages_index_path
response.status.should be(200)
end
end
describe "Home page" do
visit 'static_pages/home'
page.should have_content('OfferMe')
end
end
My spec_helper.rb file looks like this:
# 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}
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
module ::RSpec::Core
class ExampleGroup
include Capybara::DSL
include Capybara::RSpecMatchers
end
end
And, finally, my Gemfile looks like this:
source 'https://rubygems.org'
gem 'rails', '3.2.7'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
end
group :test do
gem 'capybara'
end
gem 'json'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.4'
gem 'coffee-rails', '~> 3.2.2'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
gem 'twitter-bootstrap-rails'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug'
gem 'therubyracer', '0.10.1'
gem 'libv8'
gem 'webrat'
I have seen this GitHub issue and have tried some of the workarounds as well as looked at some other StackOverflow questions pertaining to the similar issue (that's why some of the code looks redundant/hacky). However, none of them have worked yet. Please help!
UPDATE:
The problem is that you're calling visit outside of an it block, here:
describe "Home page" do
visit 'static_pages/home'
page.should have_content('OfferMe')
end
Wrap those middle lines in an it block:
describe "Home page" do
it "has a homepage" do
visit 'static_pages/home'
page.should have_content('OfferMe')
end
end
That should work.
ORIGINAL ANSWER:
I could be wrong, but I believe you have to include capybara in both the test and development sections of your Gemfile.
Try changing that section of your Gemfile to this:
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
gem 'capybara'
end
This is an old post. I have had the same issue. I thought I share my solution.
In my Gemfile I have had
group :test do
gem 'capybara'
end
And the visit was in the right spot however I was getting the same error. so this is what I did. I ran the update on the Gem and all are good now.
bundle update capybara
Hope this help someone.
Related
I don't want to run any Factories before I seed the database for some tests.
When I add require: false to factory_girl_rails I get the following error:
rails_helper.rb:37:in `block in <top (required)>': uninitialized constant FactoryGirl::Syntax (NameError)
Gem file:
source 'https://rubygems.org'
gem 'rails', '4.2.6'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'puma'
gem 'bootstrap-sass'
gem 'simple_form'
gem 'redcarpet' #This gem parses markup language to HTML
gem 'devise'
gem 'email_validator'
gem 'date_validator'
gem 'rolify'
gem 'country_select'
group :development do
gem 'web-console'
gem 'better_errors'
end
group :development, :test do
gem 'sqlite3'
gem 'byebug'
gem 'spring'
end
group :test do
gem 'spring-commands-rspec'
gem 'rspec-rails'
gem 'factory_girl_rails', require: false
gem 'faker'
gem 'capybara'
gem 'launchy'
gem 'selenium-webdriver'
gem 'database_cleaner'
gem 'shoulda-matchers', require: false
end
group :production do
gem 'pg'
gem 'rails_12factor'
end
ruby "2.2.3"
rails_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__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'shoulda_helper'
require 'shoulda/matchers'
require 'devise'
require 'support/controller_macros'
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
#this allow me to not have to write FactoryGirl inside my tests
config.include FactoryGirl::Syntax::Methods
# 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
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
config.include Devise::TestHelpers, type: :controller
config.extend ControllerMacros, type: :controller
end
You have to require FactoryGirl so you can use it.
So one way would be deleting the require: false and the other way would be to require it in the rails_helper.rb using
require 'factory_girl_rails'
I am a newbie in Ruby on Rails, when I try method let in static_pages_specs.rb
require `spec_helper`
describe "Static pages" do
let(:base_title) { "Ruby on Rails Tutorial Sample App" }
describe "Home page" do
it "should have the title 'Home'" do
visit '/static_pages/home'
expect(page).to have_title("#{base_title} | Home")
end
end
end
I get an error
Failure/Error: visit '/static_pages/home'
ActionView::Template::Error:
undefined method `let' for Class:0x000000034b2230>:0x0000000374a6c8>
My Gemfile look like this
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '4.0.4'
group :development, :test do
gem 'sqlite3'
gem 'rspec-rails'
end
group :test do
gem 'selenium-webdriver'
gem 'capybara'
end
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Also my spec_helper.rb look like this
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# 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
config.order = "random"
config.include Capybara::DSL
end
Do you have any ideas what am I missing? I am using Ubuntu 13.04.
Edit: Already tried let bang method but it still gets the same error.
In the previous post I forgot to put
require 'spec_helper'
in, but my code actually DID have it.
Sorry about that but it was not actually the problem there.
Solved: Thank you guys I've just solved it, it was just a silly typing in one of my templates.
Your static_pages_specs.rb is missing require 'spec_helper':
require `spec_helper`
describe "Static pages" do
...
end
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
Please forgive any shortcomings in this (my first-ever) post on StackOverflow. I'm brand new to Ruby on Rails. I'm following the Rails Tutorial. I have spent many unsuccessful hours consulting other threads discussing the same Name Error that I'm raising in this question.
Any attempt of mine to run an rspec test like so: $bundle exec rspec spec/models/user_spec.rb
throws the now infamous error: `': uninitialized constant Rails (NameError)
Let me know if there's any more information I should provide you in order to get the ball rolling.
Here's my gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'
gem 'faker', '1.1.2'
gem 'will_paginate', '3.0.4'
gem 'bootstrap-will_paginate', '0.0.9'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
# The following optional lines are part of the advanced setup.
# gem 'guard-rspec', '2.5.0'
# gem 'spork-rails', '4.0.0'
# gem 'guard-spork', '1.5.0'
# gem 'childprocess', '0.3.6'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.0'
gem 'cucumber-rails', '1.4.0', :require => false
gem 'database_cleaner', github: 'bmabey/database_cleaner'
# Uncomment these lines on Linux.
# gem 'libnotify', '0.8.0'
# Uncomment these lines on Windows.
# gem 'rb-notifu', '0.0.4'
# gem 'win32console', '1.3.2'
# gem 'wdm', '0.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
Here is my spec/models/user_spec.rb file:
require 'spec_helper'
describe User do
pending "add some examples to (or delete) #{__FILE__}"
end
Here is my app/models/user.rb file:
class User < ActiveRecord::Base
end
Here is my spec_helper.rb file:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
require 'test/unit'
require 'spec_helper'
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
I have definitely run bundle install. I can also confirm that I've already created the database and run the migration (db/test.sqlite3 already exists)
In your spec_helper.rb, you have the following line twice:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Delete the first instance (the one on line 2). This is what is causing the error.
Having this line before require 'rspec/rails' will cause problems because we don't know what Rails is, and so we cannot call the root method.
The second instance (on line 13) is fine because this is after require 'rspec/rails'.
Remove redundant require 'spec_helper' line from your spec_helper.rb file.
I need help figuring out why my rspec tests are running slowly.
I am running spork, guard, and rspec. However, it appears that spork is re-initializing rails each time. I am having a difficult time figuring out how to debug this issue. I suspect some gem I am using is causing this but have no proof.
The reason I think it is reloading is:
It takes the same amount of time to run inside of spork or outside
I get this warning (related to the SOLR gem) that I get when I run
rails server
Running all specs
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Sunspot::Rails::SolrInstrumentation instead. (called from at /home/dougt/replyinline/config/environment.rb:5)
Here is my 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.
# 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'
# 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
config.include Devise::TestHelpers, :type => :view
#https://github.com/plataformatec/devise/wiki/How-To:-Controllers-and-Views-tests-with-Rails-3-(and-rspec)
config.extend ControllerMacros, :type => :controller
config.extend ControllerMacros, :type => :helper
config.extend ControllerMacros, :type => :view
end
end
Spork.each_run do
# This code will be run each time you run your specs.
#https://github.com/sporkrb/spork/issues/37
# silence_warnings do
# Dir["#{Rails.root}/app/models/**/*.rb"].each {|f| load f}
# end
# load "#{Rails.root}/config/routes.rb"
# Dir["#{Rails.root}/app/**/*.rb"].each {|f| load f}
end
Gemfile
source 'http://rubygems.org'
# 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'
# gem "less-rails", "~> 2.1.4"
gem 'twitter-bootstrap-rails',"~> 2.0.3"
end
group :test do
# Pretty printed test output
gem 'spork-rails'
gem 'rspec-rails', '2.8'
gem 'guard-spork'
gem 'rspec'
gem 'growl'
gem 'rb-fsevent'
gem 'guard-spork'
end
group :development do
gem 'guard'
gem 'guard-rspec'
gem 'turn', '~> 0.8.3', :require => false
gem 'webrat', '0.7.3'
gem 'therubyracer'
gem 'progress_bar'
gem 'sunspot_solr'
gem 'minitest'
gem 'rspec-rails', '2.8'
end
gem 'rails', '3.2.2'
gem 'jquery-rails'
gem 'pg'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
gem 'execjs'
gem 'devise'
gem "cancan"
gem "oauth"
gem "oauth2"
gem "oauth-plugin", ">= 0.4.0.rc2"
gem "autotest"
gem "autotest-rails-pure"
gem "autotest-notification"
gem 'uuidtools'
gem 'json'
gem 'twitter'
gem 'ZenTest', '4.6.0'
gem 'mobylette'
gem 'diff-lcs'
gem 'twitter-text'
gem 'chronic',:git => 'git://github.com/tarr11/chronic.git'
gem 'sunspot_rails'
gem 'delayed_job_active_record', "~> 0.3.2"
gem 'delayed_task'
gem 'codemirror-rails', "~> 2.22"
gem 'thin'
gem "paperclip", "~> 2.0"
gem 'aws-sdk'
gem 'aws-s3'
gem 'newrelic_rpm'
gem 'airbrake'
gem 'factory_girl_rails', "~> 1.2"
gem 'diff_match_patch'
gem 'draper'
gem "email_veracity", "~> 0.6.0"
Guardfile
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch(%r{^config/environments/.+\.rb$})
watch(%r{^config/initializers/.+\.rb$})
watch('Gemfile')
watch('Gemfile.lock')
watch('spec/spec_helper.rb') { :rspec }
watch('test/test_helper.rb') { :test_unit }
watch(%r{features/support/}) { :cucumber }
end
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end
Guard Terminal Output
dougt#dougt-ThinkPad-W520 ~/project $ bundle exec guard
Guard uses NotifySend to send notifications.
Guard is now watching at '/home/dougt/project'
Starting Spork for RSpec
Using RSpec, Rails
Preloading Rails environment
Loading Spork.prefork block...
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Sunspot::Rails::SolrInstrumentation instead. (called from <top (required)> at /home/dougt/project/config/environment.rb:5)
Spork is ready and listening on 8989!
Spork server for RSpec successfully started
Guard::RSpec is running, with RSpec 2!
Running all specs
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Sunspot::Rails::SolrInstrumentation instead. (called from <top (required)> at /home/dougt/project/config/environment.rb:5)
/home/dougt/project/app/models/dropbox_token.rb:7: warning: already initialized constant ACCESS_TYPE
/home/dougt/project/app/models/dropbox_token.rb:9: warning: already initialized constant DROPBOX_SETTINGS
*.........**.....................***....***.....................***......*......................*
Pending:
UserMailer add some examples to (or delete) /home/dougt/project/spec/mailers/user_mailer_spec.rb
# No reason given
# ./spec/mailers/user_mailer_spec.rb:4
PersonNote add some examples to (or delete) /home/dougt/project/spec/models/person_note_spec.rb
# No reason given
# ./spec/models/person_note_spec.rb:4
Task add some examples to (or delete) /home/dougt/project/spec/models/task_spec.rb
# No reason given
# ./spec/models/task_spec.rb:4
Alert add some examples to (or delete) /home/dougt/project/spec/models/alert_spec.rb
# No reason given
# ./spec/models/alert_spec.rb:4
Slide add some examples to (or delete) /home/dougt/project/spec/models/slide_spec.rb
# No reason given
# ./spec/models/slide_spec.rb:4
BetaTester add some examples to (or delete) /home/dougt/project/spec/models/beta_tester_spec.rb
# No reason given
# ./spec/models/beta_tester_spec.rb:4
SlideContent add some examples to (or delete) /home/dougt/project/spec/models/slide_content_spec.rb
# No reason given
# ./spec/models/slide_content_spec.rb:4
SharedFile add some examples to (or delete) /home/dougt/project/spec/models/shared_file_spec.rb
# No reason given
# ./spec/models/shared_file_spec.rb:4
FileActivity add some examples to (or delete) /home/dougt/project/spec/models/file_activity_spec.rb
# No reason given
# ./spec/models/file_activity_spec.rb:4
EmailHelper add some examples to (or delete) /home/dougt/project/spec/helpers/email_helper_spec.rb
# No reason given
# ./spec/helpers/email_helper_spec.rb:14
TodoLinesHelper add some examples to (or delete) /home/dougt/project/spec/helpers/todo_lines_helper_spec.rb
# No reason given
# ./spec/helpers/todo_lines_helper_spec.rb:14
UserHelper add some examples to (or delete) /home/dougt/project/spec/helpers/user_helper_spec.rb
# No reason given
# ./spec/helpers/user_helper_spec.rb:14
TasksHelper add some examples to (or delete) /home/dougt/project/spec/helpers/tasks_helper_spec.rb
# No reason given
# ./spec/helpers/tasks_helper_spec.rb:14
todo_lines/show.html.erb add some examples to (or delete) /home/dougt/project/spec/views/todo_lines/show.html.erb_spec.rb
# No reason given
# ./spec/views/todo_lines/show.html.erb_spec.rb:4
Finished in 7.71 seconds
Running: spec/models/todo_file_spec.rb
DEPRECATION WARNING: The InstanceMethods module inside ActiveSupport::Concern will be no longer included automatically. Please define instance methods directly in Sunspot::Rails::SolrInstrumentation instead. (called from <top (required)> at /home/dougt/project/config/environment.rb:5)
/home/dougt/project/app/models/dropbox_token.rb:7: warning: already initialized constant ACCESS_TYPE
/home/dougt/project/app/models/dropbox_token.rb:9: warning: already initialized constant DROPBOX_SETTINGS
.....................
Finished in 3.05 seconds
21 examples, 0 failures
You can profile spork and use the results to require stuff in the prefork block
http://www.opinionatedprogrammer.com/2011/02/profiling-spork-for-faster-start-up-time/
There is also an issue with Devise and possibly other gems calling reload, these methods can be trapped by adding the following to your spec_helper prefork block:
...
ENV["RAILS_ENV"] ||= 'test'
# For Devise
require "rails/application"
Spork.trap_method(Rails::Application, :reload_routes!)
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
...