Ruby on rails: Undefined method 'let' - ruby-on-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

Related

Factory Girl crashing tests and console

So I have an issue with Factory Girl, I've been using it quite a bit and this is the first Rails app that I've had this issue. Basically what's happening is when I use it in the console, it crashes with the message /home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/pry-0.10.3/lib/pry/last_exception.rb:54:in 'bt_source_location_for': undefined method '[]' for nil:NilClass (NoMethodError) and a backtrace that consists entirely of gem libraries. When I go to test, it also fails. In the test initializer support directory in spec/support/factory_girl.rb I included the appropriate code to lint the factories that I always do with DatabaseCleaner:
RSpec.configure do |config|
config.before(:suite) do
begin
DatabaseCleaner.start
FactoryGirl.lint
ensure
DatabaseCleaner.clean
end
end
end
This causes an error when the tests are run: /home/vagrant/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/spring-1.6.3/lib/spring/application.rb:271:in 'ensure in block (2 levels) in shush_backtraces': undefined method 'reject!' for nil:NilClass (NoMethodError)
When I remove that file a new error is thrown when I first go to use the Factory Girl methods:
ActiveRecord::Base.transaction do
create(:team)
end
-
NoMethodError: undefined method `reject!' for nil:NilClass
--- Caused by: ---
fatal:
exception reentered
I've never seen the 'Caused by: error reentered message' before but it looks pretty cryptic. The version of Factory Girl is 4.5, Rails version 4.2.5, RSpec version 3.4. I just started this app so there isn't a whole lot of other code to the app but here are the rails helper, spec helper, and Gemfile.
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/matchers'
require 'simplecov'
SimpleCov.start 'rails'
include ActionDispatch::TestProcess
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
spec_helper.rb
require 'paperclip/matchers'
require 'devise'
RSpec.configure do |config|
config.include Paperclip::Shoulda::Matchers
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Gemfile
source 'https://rubygems.org'
gem 'coffee-rails', '~> 4.1.0'
gem 'devise'
gem 'jbuilder', '~> 2.0'
gem 'jquery-rails'
gem 'jwt'
gem 'paperclip', '~> 4.3'
gem 'pg', '~> 0.15'
gem 'rails', '~> 4.2.5'
gem 'sass-rails', '~> 5.0'
gem 'turbolinks'
gem 'uglifier', '>= 1.3.0'
gem 'unicorn'
group :test, :development do
gem 'dotenv-rails'
gem 'bullet'
gem 'did_you_mean', '~> 0.10.0'
gem 'factory_girl_rails'
gem 'pry-rails'
gem 'rspec-rails'
end
group :test do
gem 'database_cleaner'
gem 'shoulda-matchers', require: false
gem 'simplecov', :require => false
end
group :development do
gem 'active_record_query_trace'
gem 'better_errors'
gem 'binding_of_caller'
gem 'spring'
gem 'spring-commands-rspec'
end
I found the issue. In my factory I referenced an association from the has many sign where it's only supposed to be on the belongs to side. Not a great error message for that though.
FactoryGirl.define do
factory :user do
association :team
end
factory :team do
association :user
end
end
class User < ActiveRecord::Base
belongs_to :team
end
class Team < ActiveRecord::Base
has_many :users
end
The team should not have the association line.

ArgumentError in mongoid-rspec

When I do bundle exec rspec spec/controllers I got:
/var/lib/gems/1.9.1/gems/rspec-core-3.0.4/lib/rspec/core/hooks.rb:521:in `extract_scope_from': You must explicitly give a scope (example, context, suite) or scope alias (each, all) when using symbols as metadata for a hook. (ArgumentError)
My Gemfile:
gem 'rails', '4.1.4'
gem 'mongoid', '~> 4', github: 'mongoid/mongoid'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
group :development, :test do
gem 'rspec-rails', '~> 3.0.2'
end
group :test do
gem 'database_cleaner'
gem 'factory_girl_rails'
gem 'mongoid-rspec'
end
My spec_helper.rb:
# spec_helper.rb
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|
config.mock_with :rspec
config.before :each do
Mongoid.purge!
end
require 'database_cleaner'
config.before(:siute) do
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.orm = "mongoid"
end
config.before(:each) do
DatabaseCleaner.clean
end
end
My rails_helper.rb
# 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'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
RSpec.configure do |config|
# ArgumentError whether the following is commented out or not
config.infer_spec_type_from_file_location!
end
My spec/controllers/typer_controller_spec.rb (only file in the controllers folder)
require "rails_helper"
RSpec.describe TyperController, type:controller do
describe "GET #index" do
it "initializes properly" do
get :index
# nothing to test yet
end
end
end
You have a syntax error here
RSpec.describe TyperController, type:controller do
Change it to
RSpec.describe TyperController, type: :controller do
My mistake:
config.before(:siute) do
should be
config.before(:suite) do

Rspec + Capybara: controller missing, integration spec passes anyway

I started a fresh new rails app (using rspec-rails 2.99 & capybara 2.3.0), and wrote a silly simple integration spec to get started with TDD:
# spec/integration/visit_home_page_spec.rb
require 'spec_helper'
feature "view the home page" do
scenario "user visits home page" do
visit root_path
expect(page).to have_content 'Password'
end
end
As expected, the test fails due to root_path being undefined - so I went ahead and:
# config/routes.rb
root 'users#new'
And here's the problem: instead of the spec failing due to UsersController being undefined, it's green. It appears that rspec is evaluating the error page itself, and since that page happens to contain the word "password", the spec passes.
To make sure that's what's happening, I have modified the spec to:
# spec/integration/visit_home_page_spec.rb
...
expect(page).to have_content 'theresnowaythisisinthepage'
...
Now the test fails, but not because it's picking up the undefined UsersController - just because the content is not in the error page.
I've started many an app with this approach, and it's the first time I've seen rspec mistaking an error page for a valid one - what did I break?
I suspect my forehead will have a big hand-shaped mark when I find the answer.
--
Here's my Gemfile
source 'https://rubygems.org'
gem 'rails', '~> 4.1.1'
gem 'mysql2', '~> 0.3.16'
gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'active_model_serializers', '~> 0.8.1'
gem 'slim-rails', '~> 2.1.4'
gem 'bootstrap-sass', '~> 3.1.1.1'
gem 'bootstrap-generators', '~> 3.1.1.3'
group :test, :development do
gem 'rspec-rails', '~> 2.99'
gem 'capybara', '~> 2.3.0'
end
group :development do
gem "better_errors"
gem "binding_of_caller" # required by better-errors
gem "launchy"
end
group :doc do
gem 'sdoc', require: false
end
Here's spec_helper
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
config.infer_spec_type_from_file_location!
config.include Capybara::DSL
end

bundle exec rspec spec/requests/static_pages_spec.rb -- RubyonRails Tutorial

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

rspec testing throws Uninitialized constant Rails (NameError)

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.

Resources