4

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 を使用して既に構成されていると思いましたか? 私は困惑しています、何が欠けていますか?

4

4 に答える 4

9

私はこれを打った。私の古い(RSpec 1頃)仕様のいくつかには、次のrequireステートメントが含まれていることが判明しました。

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

新しい仕様のほとんどには、次のrequireステートメントがあります。

require 'spec_helper'

私は古いスタイルを片付けることを決して気にしませんでした。これが意味するのは、spec_helper.rbファイル名が2つの異なる方法でrequireに渡されたということです。1つはフルパス、もう1つはローカルパスです。これにより、spec_helper.rbが2回実行され、エラーが発生しました。すべてのrequireステートメントを短い新しいスタイルに変更すると、問題が修正されました。

于 2011-10-29T07:27:52.103 に答える
4

Railsアプリでこの問題を解決しました。

require 'spec_helper'私の問題は、2 つの仕様ファイルでファイルの先頭の行が欠落していたことです。

于 2011-11-02T06:41:42.833 に答える
2

私は同じ問題に遭遇しました。私の根本的な原因は、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 ファイルを削除します。

于 2011-12-16T05:45:49.297 に答える
1

の問題である可能性がありDir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}ます。アプリのconfigブロックの下に配置しましたが、機能しました。

于 2011-10-24T10:28:09.887 に答える