rspec testing throws Uninitialized constant Rails (NameError) - ruby-on-rails

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.

Related

Why am I getting uninitialized constant FactoryGirl::Syntax (NameError) when setting gem to require: false?

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'

When using Spork, I have both a rails_helper and spec_helper?

UPDATED 11:10am OCT 7th 2014
orginal
_I am unsure how to set up spork with rails_helper and spec_helper. I am also using guard in my stack._
I have tried different combos, and I am still having difficulty installing it.
Railscasts and Tuts+ were no help ( this time )
anyone know of an easier way to set this up?
:Gemfile
source 'https://rubygems.org'
gem 'coffee-rails', '~> 4.0.0'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'pg'
gem 'rails', '4.1.6'
gem 'sass-rails', '~> 4.0.3'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'simple_form'
gem 'spring', group: :development
gem 'turbolinks'
gem 'uglifier', '>= 1.3.0'
group :development, :test do
gem 'better_errors'
gem 'binding_of_caller'
gem 'capybara'
gem 'factory_girl_rails'
gem 'growl'
gem 'guard-rspec'
gem 'guard-spork'
gem 'meta_request'
gem 'pry-rails'
gem 'rspec-rails'
gem 'spork','1.0.0rc0'
gem 'spork-rails'
gem 'terminal-notifier-guard'
end
my rails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
my spec_helper.rb
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
I have set this up again. and now it takes for ever for the load to happen. Its as if thet rails environment isn't even loaded.
Thoughts?
I was able to get guard and spork working with rspec3. Here are the files I used:
my_rails_project/.rspec (created by rails generate rspec:install):
--color
--require spec_helper
my_rails_project/spec/rails_helper.rb (created by rails generate rspec:install):
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# 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 migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending!
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
# 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!
end
my_rails_project/spec/spec_helper.rb (created by rails generate rspec:install):
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'
#require 'rspec/autorun' #Me: This line produced a deprecation warning in the middle of test output
# 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"
#Added by ror_tut
config.include Capybara::DSL
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
my_rails_project/Guardfile:
source: Ruby on Rails Tutorial (Hartl) with changes made according to these posts:
1) Guard Rspec :cli option is deprecated, change to :cmd option
2) spork 0.9.2 and rspec 3.0.0 = uninitialized constant RSpec::Core::CommandLine (NameError)
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
require 'active_support/inflector'
guard 'rspec', all_after_pass: false, cmd: 'rspec' 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" }
# Custom Rails Tutorial specs
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m|
["spec/routing/#{m[1]}_routing_spec.rb",
"spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb",
"spec/acceptance/#{m[1]}_spec.rb",
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
"spec/requests/#{m[1].singularize}_pages_spec.rb")]
end
watch(%r{^app/views/(.+)/}) do |m|
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" :
"spec/requests/#{m[1].singularize}_pages_spec.rb")
end
watch(%r{^app/controllers/sessions_controller\.rb$}) do |m|
"spec/requests/authentication_pages_spec.rb"
end
#Rails example continued...
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
end
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' },
:rspec_env => { 'RAILS_ENV' => 'test' } do
watch('config/application.rb')
watch('config/environment.rb')
watch('config/environments/test.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
Then in your files that contain the specs, e.g. my_rails_projects/spec/requests/static_pages_spec.rb, use the following require:
require 'rails_helper'
Notice that rails_helper.rb has the line:
require 'rspec_helper.rb'
...so you get both files with require rails_helper.
my_rails_project/Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=sample_app2_gems
gem 'rails', '4.0.8'
gem 'pg', '0.15.1'source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=sample_app2_gems
gem 'rails', '4.0.8'
gem 'pg', '0.15.1' #The version in rails tutorial Gemfile, latest is:
#gem 'pg', '0.17.1'
#For Bootstrap css
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'bcrypt-ruby', '3.1.2'
group(:development, :test) do
#gem 'sqlite3', '1.3.8'
#gem 'rspec-rails', '2.13.1'
gem 'rspec-rails', '~>3.0'
gem 'guard-rspec'
gem 'spork-rails'
gem 'guard-spork'
gem 'childprocess', '0.3.6'
gem 'factory_girl_rails', '4.2.0'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara'
gem 'growl', '1.0.3'
end
gem 'sass-rails', '4.0.3'
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 'rails_12factor', '0.0.2'
end
#gem 'pg', '0.17.1'
#For Bootstrap css
gem 'bootstrap-sass', '2.3.2.0'
gem 'sprockets', '2.11.0'
gem 'bcrypt-ruby', '3.1.2'
group(:development, :test) do
#gem 'sqlite3', '1.3.8'
#gem 'rspec-rails', '2.13.1'
gem 'rspec-rails', '~>3.0'
gem 'guard-rspec'
gem 'spork-rails'
gem 'guard-spork'
gem 'childprocess', '0.3.6'
gem 'factory_girl_rails', '4.2.0'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara'
gem 'growl', '1.0.3'
end
gem 'sass-rails', '4.0.3'
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 'rails_12factor', '0.0.2'
end
As for the concerns mentioned in the second link that eliminating --drb in the Guardfile turns off spork:
Before doing $ bundle exec guard:
$ time bundle exec rspec spec/
No DRb server is running. Running in local process instead ...
.........................................
Finished in 1.24 seconds
41 examples, 0 failures
Randomized with seed 20709
real 0m6.186s
user 0m5.082s
sys 0m1.018s
Then starting guard in another terminal window:
$ bundle exec guard
Then doing this again:
$ time bundle exec rspec spec/
.........................................
Finished in 1.66 seconds
41 examples, 0 failures
Randomized with seed 7823
real 0m3.145s
user 0m1.015s
sys 0m0.178s
...you can see there was a speed up.

Rspec view undefined method stub_model

i'm trying to find out what is wrong with this code. Namely it cannot find method 'stub_model'. Tried to look for solution for this but everywhere i look my files seems to be good. Please take a look on it maybe i just can't see a simple mistake. Thanks a lot :)
Book model is created in db.
My view spec (spec/view/books_spec.rb) looks like this:
require 'rails_helper'
describe 'books/new' do
it 'displays the book form' do
book = stub_model(Book)
assign(:book, book)
render
expect(rendered).to have_selector("form label[for *= 'Title']")
expect(rendered).to have_selector("form label[for *= 'Author']")
expect(rendered).to have_selector("form label[for *= 'Cover Photo']")
expect(rendered).to have_button "Add Book"
end
end
and the error is following:
1) books/new displays the book form
Failure/Error: book = stub_model(Book)
NoMethodError:
undefined method `stub_model' for #<RSpec::ExampleGroups::BooksNew:0x00000103e61870>
# ./spec/views/books_spec.rb:6:in `block (2 levels) in <top (required)>'
My rails_helper.rb looks like:
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
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. 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 with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
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|
# 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!
end
and my Gemfile.rb:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.2.rc1'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.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
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
group :test do
gem 'cucumber-rails', :require => false
gem 'database_cleaner'
end
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
end
# 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
# Use debugger
# gem 'debugger', group: [:development, :test]
What is your rspec-rails version?
According to the Changelog and this Commit mock_model and stub_model are removed since version 3.0.0 of rspec-rails.
rspec mocks are externalized in an another gem rspec-activemodel-mocks . You should include it in your Gemfile and try it.
Hope it helps
this should be a comment to ahnbizcad's comment, but I lack the reputation to do it properly.
It should be fixed by adding
require 'rspec/active_model/mocks'
to spec_helper.rb

Ruby on rails: Undefined method 'let'

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

RSpec 2.13.1 hanging in Ruby 1.9.3 with Rails 3.2.13 and Spork 1.0.0rc3

Trying to resurrect an old project by upgrading gems. Ran into an issue where RSpec hangs on startup with the '--drb' option.
By itself, 'rspec spec' works fine. But start 'spork' in another terminal and then 'rspec --drb spec' spins up CPU to ~40% and just sits.
Using Rails 3.2.13, Ruby 1.9.3-p392, RSpec 2.13.1 and spork-rails 3.2.1, which depends on Spork 1.0.0rc3.
Gemfile
source 'http://rubygems.org'
gem 'rails', '~> 3.2.13'
gem 'devise', '~> 2.2.0'
gem 'event-calendar', :require => 'event_calendar'
gem 'has_scope'
gem 'high_voltage'
gem 'inherited_resources'
gem 'jquery-rails'
gem 'kaminari'
gem 'ransack'
gem 'simple_form'
gem 'validates_timeliness'
# deployment process management
gem 'foreman'
gem 'thin'
# 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', '< 2.0'
gem 'bootstrap-sass', '~> 2.3.1'
end
group :production do
gem 'pg'
end
group :development, :test do
gem 'factory_girl_rails', '~> 4.2.0'
gem 'rspec-rails', '~> 2.13.1'
gem 'spork-rails'
gem 'sqlite3'
end
group :test do
gem 'capybara'
gem 'growl'
gem 'launchy'
gem 'ruby-prof'
gem 'test-unit'
end
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
# Prevent Devise from loading the User model super early with it's routes
# see: https://github.com/sporkrb/spork/wiki/Spork.trap_method-Jujitsu
require "rails/application"
Spork.trap_method(Rails::Application::RoutesReloader, :reload!)
# 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'
# Since I have included Test::Unit for perfomance tests, disable auto-run
Test::Unit::AutoRunner.need_auto_run = false if defined?(Test::Unit::AutoRunner)
# 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
# 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
end
Spork.each_run do
# This code will be run each time you run your specs.
end
I'm not getting any error messages or feedback on the console, so I don't even know where to start looking for problems. Any suggestions?
Upgrading ruby-build via brew update ruby-build picked up a new version dated 2013-05-01. After the update, I trashed and reinstalled all my Rubies, and the problem seems to have resolved itself.
I don't know what specifically addressed the issue, but here are some notes and a relevant commit: https://github.com/sstephenson/ruby-build/commit/9f8d53365aef52c940095f583cdc82f02caba90f

Resources