NameError: uninitialized constant Minitest::VERSION - ruby-on-rails

I'm using Rails 4.1 and Ruby 2.0.0. I'm trying to set up testing with minitest-rails and I'm running into this strange error. When I include:
require 'minitest/spec'
In my 'spec_helper' file it give me a NameError: uninitialized constant Minitest::VERSION error. When I comment out this line, everything seems to work fine. The odd thing is that 'minitest/autorun' is also in there and not causing any problems. Maybe you guys can shed some light on what's going on here.
spec_helper.rb:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'minitest/spec'
require 'minitest/autorun'
require 'minitest-rails'
require 'minitest-rails-capybara'
Rakefile:
require File.expand_path('../config/application', __FILE__)
Pinteresting::Application.load_tasks
namespace :test do
task :run do
ENV["RACK_ENV"] = "test"
$LOAD_PATH.unshift("lib", "spec")
if ARGV[1]
require_relative ARGV[1]
else
Dir.glob("./spec/**/*_spec.rb").each { |file| require file }
end
end
end
.spec:
require "spec_helper"
describe "Test" do
describe "When two is equal to two" do
it "asserts true" do
assert_equal(2, 2)
end
end
end
Stack trace:
nbp-93-202:pinteresting Frank$ rake test:run
rake aborted!
NameError: uninitialized constant Minitest::VERSION
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:22:in `<class:Unit>'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:21:in `<module:Minitest>'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/unit.rb:20:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p481/gems/minitest-5.3.4/lib/minitest/spec.rb:1:in `<top (required)>'
/Users/Frank/Desktop/pinteresting/spec/spec_helper.rb:4:in `<top (required)>'
/Users/Frank/Desktop/pinteresting/spec/diagnostic_spec.rb:1:in `<top (required)>'
/Users/Frank/Desktop/pinteresting/Rakefile:12:in `block (3 levels) in <top (required)>'
/Users/Frank/Desktop/pinteresting/Rakefile:12:in `each'
/Users/Frank/Desktop/pinteresting/Rakefile:12:in `block (2 levels) in <top (required)>'
Tasks: TOP => test:run

Interestingly, if try to run or require a file with just the two requires minitest/spec and minitest/autorun the interpreter raises a warning saying that you should require 'minitest/autorun' instead or add "gem 'minitest'" before require 'minitest/autorun', although it doesn't rise the NameErrorto me.
So switching the require statements around (in order to first require minitest/autorun) seems to do the trick. Requiring minitestin the first place seems to do the trick also.

I think you can resolve this warning by making your implementation simpler. In spec/spec_helper.rb:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/rails"
require "minitest/rails/capybara"
You are missing the require for rails/test_help. Did you remove that for a specific reason?
In Rakefile:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
Rails::TestTask.new("test:spec" => "test:prepare") do |t|
t.pattern = "spec/**/*_spec.rb"
end
Rake::Task["test:run"].enhance ["test:spec"]
And now run either $ rake test:spec to run all your specs, or $ rake test to run all your tests. The reason behind keeping the rake tasks under the test namespace is because this is what Spring keys on to use the running test environment. Spring uses the task namespace, not the directory name.

Related

NoMethodError: undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class

Searching for this problem has only turned up issues with specific gems or classes that other people have dealt with. In my case, I think there's something wrong with rspec in general.
Whenever I generate a controler with rails generate ControllerName, it sets everything up and things seem to work fine. I've routed a few controllers and tested them in development and production and everything works.
The only thing that seems to be broken is testing them with rspec. Right now I have two controllers in my project, and whenever I run rspec/spec, most specs generated by rails g turns up this error:
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::MoreStuffHere
For example, I have a WelcomeController and ApplicationsController and these are the errors I keep getting:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeController:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeAboutHtmlErb:Class
undefined method `setup' for RSpec::ExampleGroups::WelcomeIndexHtmlErb:Class
Interestingly, I don't get errors for their helper_spec's
Here is a full error in case this helps:
An error occurred while loading ./spec/controllers/applications_controller_spec.rb.
Failure/Error:
RSpec.describe ApplicationsController, type: :controller do
end
NoMethodError:
undefined method `setup' for RSpec::ExampleGroups::ApplicationsController:Class
# ./spec/controllers/applications_controller_spec.rb:3:in `<top (required)>'
Does anyone have any idea where this issue could lie?
In spec_helper.rb I had
require File.expand_path("../../config/environment", __FILE__)
require 'rails/all'
RSpec.configure do |config|
# More code here.
end
All I had to do was add require 'rspec/rails' under require 'rails/all'. This solved my problem.
Yet, I don't know why. If someone can elaborate that would be great. I already had require 'rspec/rails' in rails_herlper.rb, but obviously that wasn't good enough.
IMHO the solution here is somewhat what Damian Rivas says there, but the important thing is:
in rails helper you have a line:
require 'spec_helper'
in spec_helper you have in config block line to include devise helpers:
config.include Devise::Test::ControllerHelpers, type: :controller
you also as said above have to have rspec/rails require line
The main point there was that you have to watch the order here... always including rspec/rails has to go before spec_helper
So lets say example spec_helper.rb looks like:
require 'rubygems'
Rspec.configure do |config|
config.include Devise::Test::ControllerHelpers, type: :controller
end
Then our rails_hepler.rb has to look like:
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'spec_helper'
Then in sample_spec.rb you require rails_helper, or in my case I require through .rspec so I don't have to have it on top of every spec.
Hope it helps somebody :)

Rails minitest devise error

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.

Rails 4, RSpec Application has been already initialized

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.

RSpec MustBeConfiguredBeforeExampleGroupsError on plain project

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.

Error running cucumber tests - no such file to load

I have a rails app with Cucumber 1.0.0 and cucumber-rails 1.0.2. These tests ran fine last night. Today I just altered some step definitions and tried to run the tests and got the error below. I haven't changed anything in the cucumber support files. Any ideas?
grouperty $ cucumber
Using the default profile...
no such file to load -- /Users/davidtuite/wd/grouperty/features/config/environment(LoadError)
/Users/davidtuite/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/davidtuite/.rvm/rubies/ruby-1.9.2-head/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
/Users/davidtuite/wd/grouperty/features/support/env.rb:12:in `block in <top (required)>'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/spork-0.9.0.rc9/lib/spork.rb:24:in `prefork'
/Users/davidtuite/wd/grouperty/features/support/env.rb:10:in `<top (required)>'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/rb_support/rb_language.rb:143:in `load'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/rb_support/rb_language.rb:143:in `load_code_file'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:176:in `load_file'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:78:in `block in load_files!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:77:in `each'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime/support_code.rb:77:in `load_files!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime.rb:137:in `load_step_definitions'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/runtime.rb:39:in `run!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/cli/main.rb:43:in `execute!'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/lib/cucumber/cli/main.rb:20:in `execute'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/gems/cucumber-1.0.0/bin/cucumber:14:in `<top (required)>'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/bin/cucumber:19:in `load'
/Users/davidtuite/.rvm/gems/ruby-1.9.2-head#grouperty/bin/cucumber:19:in `'
Here is the content of /features/support/env.rb. Note that most of it was generated by the spork gem (v0.9.0.rc9).
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a
# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'cucumber/rails'
# Capybara defaults to XPath selectors rather than Webrat's default of CSS3. In
# order to ease the transition to Capybara we set the default here. If you'd
# prefer to use XPath just remove this line and adjust any selectors in your
# steps to use the XPath syntax.
Capybara.default_selector = :css
end
Spork.each_run do
# By default, any exception happening in your Rails application will bubble up
# to Cucumber so that your scenario will fail. This is a different from how
# your application behaves in the production environment, where an error page will
# be rendered instead.
#
# Sometimes we want to override this default behaviour and allow Rails to rescue
# exceptions and display an error page (just like when the app is running in production).
# Typical scenarios where you want to do this is when you test your error pages.
# There are two ways to allow Rails to rescue exceptions:
#
# 1) Tag your scenario (or feature) with #allow-rescue
#
# 2) Set the value below to true. Beware that doing this globally is not
# recommended as it will mask a lot of errors for you!
#
ActionController::Base.allow_rescue = false
# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin
DatabaseCleaner.strategy = :transaction
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
# You may also want to configure DatabaseCleaner to use different strategies for certain features and scenarios.
# See the DatabaseCleaner documentation for details. Example:
#
# Before('#no-txn,#selenium,#culerity,#celerity,#javascript') do
# DatabaseCleaner.strategy = :truncation, {:except => %w[widgets]}
# end
#
# Before('~#no-txn', '~#selenium', '~#culerity', '~#celerity', '~#javascript') do
# DatabaseCleaner.strategy = :transaction
# end
#
end
this line
require File.expand_path("../../config/environment", __FILE__)
requires
/Users/davidtuite/wd/grouperty/features/config/environment
which is not present there this causes error

Resources