RSpec MustBeConfiguredBeforeExampleGroupsError for a simple project

I am creating a Rails 3.1 project and would like to test it correctly with RSpec.

After running rails g rspec:install and then starting rspec , the console messages will look like this:

 % 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 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 was already configured using config.mock_with? I am puzzled, what is missing?

+4
source share
4 answers

I just hit it. It turned out that some of my old (around RSpec 1) specifications had the following requirement:

 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') 

In most new specifications, this required an operator:

 require 'spec_helper' 

I would never bother to remove the old style. This meant that the file name spec_helper.rb was transferred for use in two different ways: one full path, one local path. This, in turn, caused spec_helper.rb to run twice, causing an error. Changing all demanding instructions to a short new style fixed the problem.

+9
source

I just solved this problem for my Rails application.

My problem was that in two specification files there was no require 'spec_helper' at the top of the file.

+4
source

I met the same problem, and my main reason: there are some specification files in the spec / support folder!

eg.

 spec/support/xx_spec.rb 

which is as follows:

 require 'spec_helper' describe XX do ... 

and in spec / spec_helper.rb there is:

 Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 

This is a situation where spec_helper was recursive, required unlimited time.

So the solution is very simple:

DELETE all xx_spec.rb files from the specs / support folder.

+2
source

Maybe the problem is Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} . I placed it under the configuration block in my application and it worked.

+1
source

All Articles