I'm currently working on my first tests but I'm stuck with a problem I can't seem to fix by myself. I've searched for solutions for some hours now and found similar errors but I didn't find any solution working in my case. Maybe it's because of a version difference, I don't know.
The problem appears when I'm writing a unit test with minitest, maybe also integration tests. I'm using devise for managing users.
This is there error that appears in the terminal when I rake test:
1) Error:
test_deactivate_enrolment(EnrolmentTest):
NoMethodError: undefined method `env' for nil:NilClass
/Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.0.3/lib/devise/test_helpers.rb:24:in `setup_controller_for_warden'
/Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:429:in `_run__840473936918917337__setup__1779200460764756091__callbacks'
/Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/myname/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-3.2.13/lib/active_support/testing/setup_and_teardown.rb:35:in `run'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:919:in `block in _run_suite'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:912:in `map'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:912:in `_run_suite'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:657:in `block in _run_suites'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:655:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:655:in `_run_suites'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:867:in `_run_anything'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1060:in `run_tests'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1047:in `block in _run'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1046:in `each'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1046:in `_run'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/minitest/unit.rb:1035:in `run'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:21:in `run'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:774:in `run'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:366:in `block (2 levels) in autorun'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:27:in `run_once'
/Users/myname/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/test/unit.rb:365:in `block in autorun'
This is what my spec_helper.rb looks like:
require 'listen'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.color_enabled = true
config.order = :random
config.filter_run :focus => true
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run_excluding :broken => true
config.fail_fast = true
end
def test_latency
0.1
end
# Crash loud in tests!
Thread.abort_on_exception = true
And this is my test_helper.rb
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
#require "minitest/rails/capybara"
# Uncomment for awesome colorful output
#require "minitest/pride"
require 'minitest/autorun'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
fixtures :all
include Devise::TestHelpers
# Add more helper methods to be used by all tests here...
end
I've tried adding include Devise::TestHelpers on top of the files which fail but that didn't fix it. Deleting the line in 'test_helper.rb` had the same result. Maybe someone had this error as well?
Update
This is on of the test files, nothing too advanced...
require 'test_helper'
class EnrolmentTest < ActiveSupport::TestCase
test "deactivate enrolment" do
assert true
end
end
Only include the Devise helpers in your controller tests. Your test_helper.rb file should look like this:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
#require "minitest/rails/capybara"
# Uncomment for awesome colorful output
#require "minitest/pride"
require 'minitest/autorun'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionController::TestCase
include Devise::TestHelpers
end
Alternative, you can use Warden to test instead of Device test helpers, here there is an example of use. Devise is based on Warden.
Remove include Devise::TestHelpers from your test_helper.rb. Include it only in the test that needs it.
Related
I am trying to setup rspec (v3.0.2) on Rails 4.1.9 with factory_girl_rails but after implementing rails_helper.rb and spec_helper.rb I get the following error when running my _spec.rb file:
/Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:9:in `<module:AttributeMethods>': uninitialized constant ActiveModel::AttributeMethods (NameError)
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:7:in `<module:ActiveRecord>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/attribute_methods.rb:5:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record.rb:101:in `<module:ActiveRecord>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record.rb:31:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/railtie.rb:1:in `require'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/activerecord-4.1.9/lib/active_record/railtie.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:12:in `require'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:12:in `block in <top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:10:in `each'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/railties-4.1.9/lib/rails/all.rb:10:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/config/application.rb:3:in `require'
from /Users/user/code/rtb-campaign-optimization/config/application.rb:3:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/config/environment.rb:2:in `require'
from /Users/user/code/rtb-campaign-optimization/config/environment.rb:2:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/spec/rails_helper.rb:4:in `require'
from /Users/user/code/rtb-campaign-optimization/spec/rails_helper.rb:4:in `<top (required)>'
from /Users/user/code/rtb-campaign-optimization/spec/models/cpa_calculator_spec.rb:1:in `require'
from /Users/user/code/rtb-campaign-optimization/spec/models/cpa_calculator_spec.rb:1:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
from /Users/user/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
from /Users/user/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
Here is my gemfile:
group :development do
gem 'rails-dev-tweaks'
gem 'quiet_assets'
gem 'pry'
gem 'pry-byebug'
gem 'hirb'
gem 'spring'
gem 'spring-commands-rspec'
end
group :development, :test do
gem 'rspec-rails', '~> 3.0.0'
gem 'factory_girl_rails'
end
group :test do
gem 'database_cleaner'
end
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'
# 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`.
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
spec_helper.rb:
RSpec.configure do |config|
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# 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
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
end
my _spec file:
require 'rails_helper'
describe 'CampaignsManagement::CpaCalculator' do
let(:campaign) {
camp = FactoryGirl.build_stubbed(:campaign)
# campaign_histories_per_campaigns.times.each do |index|
# Factory.build(:campaign_history, value: index + 1, campaign_id: camp.id)
# end
camp
}
context "when there are some active campaigns" do
context "when the campaign have histories" do
let(:campaign_histories_per_campaigns) { 10 }
context 'when the campaign have histories created today' do
end
context 'when the campaign has only histories in the past (not generated today)' do
end
end
context "when the campaign is histories orphaned" do
let(:campaign_histories_per_campaigns) { 0 }
it "should not update redshift" do
campaign.campaign_histories.size.should.eq(0)
end
end
end
end
application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module RtbCampaignOptimization
class Application < Rails::Application
config.assets.enabled = false
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.autoload_paths << Rails.root.join('lib')
end
end
any ideas?
Thanks
Try to run:
rails generate rspec:install //which you must have already run
bundle install
And also check out this question and it's answers
According to the above, you need to add spork-rails to your Gemfile.
All the above-shared code snippets all look good.
I'm testing for validation of entry of a city attribute in an addresses model. My model spec is as follows
require 'rails_helper'
RSpec.describe Address, :type => :model do
before(:each) do
#valid_attributes = {
street: 'rock avenue',
city: 'MSA',
zip: '00100',
person_id: 1
} # runs before each it block
end
context "Validations" do
it 'must have a city' do
a = Address.new
expect(a.errors_on(:city)).not_to be_empty
end
end
end
When I run the spec, i get the following error
1) Address Validations must have a city
Failure/Error: expect(a.errors_on(:city)).not_to be_empty
NoMethodError:
undefined method `errors_on' for #<Address:0xd844538>
# ./spec/models/address_spec.rb:19:in `block (3 levels) in <top (required)>'
My test gems are set up as following in my Gemfile
group :development, :test do
gem 'rspec-rails'
gem 'factory_girl_rails', :require => false
end
group :test do
gem 'capybara'
gem 'guard'
gem 'guard-rspec'
gem 'libnotify'
gem 'rspec-collection_matchers'
end
I have included the rspec collection matchers in my spec_helper, so I don't understand why I am getting this error
My spec_helper.rb
require 'capybara/rspec'
require 'factory_girl_rails'
require 'rspec/collection_matchers'
However I can see that the method, errors_on has been defined in rspec-collection_matchers gem as shown here
My .rspec
--color
--require spec_helper
I have also changed the position of
require 'rspec/collection_matchers'
to rails_helper.rb, but running the test suite again brings up the same error
rails_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require 'rspec/collection_matchers'
require 'capybara/rspec'
require 'factory_girl_rails'
require '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.include Capybara::DSL
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
My spec_helper.rb is empty with only the block below:
RSpec.configure do |config|
end
I am using Rails 4.1.0, rspec 3.0.0 and rspec-collection_matchers 1.0.0
Without seeing the repo, this is only a guess based on the information. I think this is a load order problem.
It looks like you are loading rspec/collection_matchers in spec_helper, not rails_helper. If you are using the new default configuration, --require spec_helper is in your .rspec file. When you run rspec this will require spec_helper very early on. When that happens it requires rspec/collection_matchers. However, at that point rails_helper has not been loaded. Unfortunately, this also means most of ActiveSupport including ActiveModel are not loaded yet. It also means the Rails autoloading functionality has not been loaded, so those won't get defined when you reference them.
All this to say, the check for ActiveModel here returns false. Which does not load errors_on.
Try moving the require into rails_helper.
Note on testing in isolation:
If you are going for faster testing in isolation this is my suggestion:
spec_helper should be kept very minimal and lightweight, that means for the most part you shouldn't add any require declarations to it
Move the requires you listed from spec_helper into rails_helper, those are mostly Rails specific and do not belong in spec_helper as Rails is not loaded there
If you need access to the other rspec/collection_matchers functionality in specs which do not require Rails, then just add the require 'rspec/collection_matchers' to the top of the spec file; Ruby will handle making sure it is only loaded once
I have been trying to use RSpec with Rails 4 but came across this error that I can't get rid of.
However, when I run test on single *.rb files it works without any errors. I use guard and have tried to run rspec in single mode but received the same error.
13:46:26 - INFO - Running: spec
/Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:214:in `initialize!': Application has been already initialized. (RuntimeError)
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/config/environment.rb:5:in `<top (required)>'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/spec/spec_helper.rb:3:in `<top (required)>'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
from /Users/xxx/Documents/Mitt_bolag/Minnesota/beställningssite/order_site/spec/requests/products_spec.rb:1:in `<top (required)>'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `each'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:22:in `run'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
from /Users/xxx/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'
This is 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
# 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 Devise::TestHelpers, :type => :controller
end
class ActionController::TestCase
include Devise::TestHelpers
end
I ran into this problem while upgrading a Rails 4.2 and Ruby 2.2 app to Rails 4.2 and Rails 2.6. Apparently, ActionController::TestResponse is reused and therefore initialized multiple times. You can add a workaround reading this thread, or simply create spec/support/action_controller_workaround.rb:
if RUBY_VERSION>='2.6.0'
if Rails.version < '5'
class ActionController::TestResponse < ActionDispatch::TestResponse
def recycle!
# hack to avoid MonitorMixin double-initialize error:
#mon_mutex_owner_object_id = nil
#mon_mutex = nil
initialize
end
end
else
puts "Monkeypatch for ActionController::TestResponse no longer needed"
end
end
Try setting your spec_helper to 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' # NOTE: I DON'T have this but you do...
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end
I don't know if you've already got the solution, but here I go anyway:
I'm facing the same problem since yesterday. I've found that running a
single spec by time or removing the 'require "spec_helper" from all specs but one,
it would work.
And a few minutes ago, I've found that the problem was the ruby version.
If you upgrade your ruby-2.0.0-p0 to ruby-2.0.0-p247, then the problem goes away!
I hope I've been helpful.
There's quite a few solid tutorials out there, and I haven't had too much trouble getting this working in the past. But, after hours of trying, I must be missing something.
I've completed the standard installation instructions, and started up the spork server:
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
Looking good.
Then, I hop over to a new tab and run my specs:
$ rspec spec
This command returns nothing after running for only a split second. So I jump back to the spork server to see what happened and get:
Using RSpec
Preloading Rails environment
Loading Spork.prefork block...
Spork is ready and listening on 8989!
Running tests with args ["--color", "spec"]...
Exception encountered: #<DRb::DRbConnError: druby://localhost:56736 - #<Errno::ECONNREFUSED: Connection refused - connect(2)>>
backtrace:
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:736:in `rescue in block in open'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:730:in `block in open'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:729:in `each'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:729:in `open'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1191:in `initialize'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1171:in `new'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1171:in `open'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1087:in `block in method_missing'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1105:in `with_friend'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1086:in `method_missing'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1074:in `respond_to?'
/Users/briancody/.rvm/gems/ruby-1.9.3-p0#scholastica/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:10:in `block in run'
/Users/briancody/.rvm/gems/ruby-1.9.3-p0#scholastica/gems/spork-0.9.0.rc9/lib/spork/forker.rb:21:in `block in initialize'
/Users/briancody/.rvm/gems/ruby-1.9.3-p0#scholastica/gems/spork-0.9.0.rc9/lib/spork/forker.rb:18:in `fork'
/Users/briancody/.rvm/gems/ruby-1.9.3-p0#scholastica/gems/spork-0.9.0.rc9/lib/spork/forker.rb:18:in `initialize'
/Users/briancody/.rvm/gems/ruby-1.9.3-p0#scholastica/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:9:in `new'
/Users/briancody/.rvm/gems/ruby-1.9.3-p0#scholastica/gems/spork-0.9.0.rc9/lib/spork/run_strategy/forking.rb:9:in `run'
/Users/briancody/.rvm/gems/ruby-1.9.3-p0#scholastica/gems/spork-0.9.0.rc9/lib/spork/server.rb:48:in `run'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1548:in `perform_without_block'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1508:in `perform'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1586:in `block (2 levels) in main_loop'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1582:in `loop'
/Users/briancody/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/drb/drb.rb:1582:in `block in main_loop'
Done.
For some reason, drb is failing to connect? I've googled like crazy, but there just doesn't seem to be anything helpful out there.
Some possible clues
The port (in this case 56736) changes every time I attempt to run the suite
I get the same result on both ruby 1.9.2 and ruby 1.9.3
I'm using a mac (snow leopard) and firewalls are disabled
I've tried it on a totally different mac (also snow leopard) with the same result.
I'm using the latest version of cucumber, rspec, and spork.
I also setup cucumber spork and it failed the same way.
As you can see, I've been banging away at it for a while. My project uses a lot of gems and not having spork makes testing really, really terrible. Please help...
UPDATE
Here's my spec_helper:
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require "email_spec"
require 'webmock/rspec'
require 'vcr'
require 'database_cleaner'
include Devise::TestHelpers
include EmailSpec::Helpers
include EmailSpec::Matchers
# Stub all updates to search indexes
class Profile
def update_tank_indexes ; end
def delete_tank_indexes ; end
end
class Journal
def update_tank_indexes ; end
end
# Apparently rspec doesn't understand <tt>main_app</tt> so stub it out.
def main_app ; self ; end
# 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
# 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.extend VCR::RSpec::Macros
# Always clean the database after running describe/context blocks to ensure a
# consistent state. This is especially important when using the <tt>rspec-set</tt>
# method. This method creates setup object once -- and only once -- proir to
# running an entire describe/context block. In this way, it's similar to using
# <tt>before(:all)</tt> except that <tt>set</tt> is better because:
#
# * It automatically reloads the object before each example, making it much
# safer than before(:all) which can cause bugs if you're not careful.
# * It's lazily evaluated.
#
# You can read more at:
#
# * eggsonbread.com/2010/05/25/speed-up-your-specs-with-set/
# * github.com/pcreux/rspec-set
#
# Note: This is not part of default configuration. Be sure to retain this snippet
# when upgrading.
#
DatabaseCleaner.strategy = :truncation
config.before(:all) do
DatabaseCleaner.clean
end
config.after(:suite) do
DatabaseCleaner.clean
end
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
I opened a ticket for this issue, but nothing has come of it in the last 3.5 months: https://github.com/sporkrb/spork/issues/133
Update: I found the solution, and reported everything to that ticket. tl;dr: change your Gemfile entry for 'always_validate_ssl_certifitcates' to include :require => false.
Can you roll back to spork 0.8.5 and see if it works? I've just verified that working.
I'm setting up a Rails 3.1 project and like to properly test it using RSpec.
After running rails g rspec:install and further running rspec, the console messages read as follows:
% rspec
/Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:470:in `assert_no_example_groups_defined': RSpec's mock_framework configuration option must be configured before any example groups are defined, but you have already defined a group. (RSpec::Core::Configuration::MustBeConfiguredBeforeExampleGroupsError)
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:168:in `mock_framework='
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:142:in `mock_with'
from /Users/ened/project/spec/controllers/../spec_helper.rb:19
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core.rb:71:in `configure'
from /Users/ened/project/spec/controllers/../spec_helper.rb:11
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:240:in `require'
from /Users/ened/project/spec/controllers/submissions_controller_spec.rb:1
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:225:in `load_dependency'
from /Library/Ruby/Gems/1.8/gems/activesupport-3.1.0/lib/active_support/dependencies.rb:234:in `load'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:459:in `load_spec_files'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:459:in `map'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/configuration.rb:459:in `load_spec_files'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/command_line.rb:18:in `run'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/runner.rb:80:in `run_in_process'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/runner.rb:69:in `run'
from /Library/Ruby/Gems/1.8/gems/rspec-core-2.7.0/lib/rspec/core/runner.rb:10:in `autorun'
from /usr/bin/rspec:19
My rspec/spec_helper.rb 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'
# 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
I thought it's configured already using the config.mock_with ? I'm puzzled, what is missing?
I just hit this. It turned out to be that some of my old (circa RSpec 1) specs had the following require statement:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
Most of the new specs had this require statement:
require 'spec_helper'
I'd never bothered to tidy up the old style. What this meant was that the spec_helper.rb filename was passed to require in two different ways: one full path, one local path. This in turn caused spec_helper.rb to be executed twice, triggering the error. Changing all the require statements to the short new style fixed the issue.
I just resolved this problem for my Rails app.
My problem was that two spec files were missing the require 'spec_helper' line at the top of the file.
I met the same problem, and my root cause is:
there are some spec files exist in spec/support folder!
e.g.
spec/support/xx_spec.rb
which looks like:
require 'spec_helper'
describe XX do
...
and in the spec/spec_helper.rb file, there are:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
This is a situation that 'spec_helper' was recursive required for unlimited times.
So the solution is very simple:
REMOVE all the xx_spec.rb files from spec/support folder.
Could be an issue of Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}. I have placed it below the config block in my app and it worked.