【问题标题】:RSpec MustBeConfiguredBeforeExampleGroupsError on plain project普通项目上的 RSpec MustBeConfiguredBeforeExampleGroupsError
【发布时间】:2011-10-21 02:23:33
【问题描述】:

我正在设置一个 Rails 3.1 项目,并希望使用 RSpec 对其进行正确测试。

运行rails g rspec:install 并进一步运行rspec 后,控制台消息如下:

% 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

我的rspec/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}

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

我认为它已经使用 config.mock_with 进行了配置?我很困惑,缺少什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 rspec2 rspec-rails


    【解决方案1】:

    我刚刚打了这个。事实证明,我的一些旧(大约 RSpec 1)规范具有以下要求声明:

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

    大多数新规范都有这个要求声明:

    require 'spec_helper'
    

    我从不费心整理旧样式。这意味着 spec_helper.rb 文件名以两种不同的方式传递给 require:一种是完整路径,一种是本地路径。这反过来又导致 spec_helper.rb 被执行两次,从而触发了错误。将所有 require 语句更改为简短的新样式解决了该问题。

    【讨论】:

      【解决方案2】:

      我刚刚为我的 Rails 应用解决了这个问题。

      我的问题是两个规范文件缺少文件顶部的 require 'spec_helper' 行。

      【讨论】:

      • 奇怪的是,我使用的 6 台机器中只有 1 台出现此问题,并且所有机器都针对来自源代码控制的同一副本运行构建。
      • 完全一样。原因是不同的操作系统以不同的顺序加载文件。这样,在某些机器上首先加载的好文件,但在其他一些机器上,第一个加载的规范是缺少“require”语句的那个​​......
      【解决方案3】:

      我遇到了同样的问题,我的根本原因是: spec/support 文件夹中存在一些规范文件!

      例如

      spec/support/xx_spec.rb    
      

      看起来像:

      require 'spec_helper'
      describe XX do 
      ...
      

      在spec/spec_helper.rb文件中,有:

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

      这是一种“spec_helper”需要无限次递归的情况。

      所以解决方法很简单:

      从 spec/support 文件夹中删除所有 xx_spec.rb 文件。

      【讨论】:

        【解决方案4】:

        可能是Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 的问题。我已将它放在我的应用程序中的配置块下方并且它有效。

        【讨论】:

          猜你喜欢
          • 2015-07-29
          • 1970-01-01
          • 2017-08-18
          • 1970-01-01
          • 2020-11-20
          • 2020-06-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多