Phi*_*lip 2 rspec ruby-on-rails ruby-on-rails-4
我一直在尝试将 RSpec 与 Rails 4 一起使用,但遇到了这个我无法摆脱的错误。
\n\n但是,当我对单个 *.rb 文件运行测试时,它可以正常工作,没有任何错误。我使用 Guard 并尝试在单一模式下运行 rspec 但收到相同的错误。
\n\n13:46:26 - INFO - Running: spec\n/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)\n 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\'\n from /Users/xxx/Documents/Mitt_bolag/Minnesota/besta\xcc\x88llningssite/order_site/config/environment.rb:5:in `<top (required)>\'\n 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\'\n 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\'\n 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\'\n 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\'\n from /Users/xxx/Documents/Mitt_bolag/Minnesota/besta\xcc\x88llningssite/order_site/spec/spec_helper.rb:3:in `<top (required)>\'\n 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\'\n 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\'\n 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\'\n 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\'\n from /Users/xxx/Documents/Mitt_bolag/Minnesota/besta\xcc\x88llningssite/order_site/spec/requests/products_spec.rb:1:in `<top (required)>\'\n 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\'\n 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\'\n 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\'\n 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\'\n 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\'\n 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\'\n 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\'\n 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\'\n 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\'\n 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\'\n
Run Code Online (Sandbox Code Playgroud)\n\n这是我的spec_helper.rb
# This file is copied to spec/ when you run \'rails generate rspec:install\'\nENV["RAILS_ENV"] ||= \'test\'\nrequire File.expand_path("../../config/environment", __FILE__)\nrequire \'rspec/rails\'\nrequire \'rspec/autorun\'\nrequire \'capybara/rspec\'\n\n# Requires supporting ruby files with custom matchers and macros, etc,\n# in spec/support/ and its subdirectories.\nDir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }\n\n# Checks for pending migrations before tests are run.\n# If you are not using ActiveRecord, you can remove this line.\nActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)\n\nRSpec.configure do |config|\n # ## Mock Framework\n #\n # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:\n #\n # config.mock_with :mocha\n # config.mock_with :flexmock\n # config.mock_with :rr\n\n # Remove this line if you\'re not using ActiveRecord or ActiveRecord fixtures\n # config.fixture_path = "#{::Rails.root}/spec/fixtures"\n\n # If you\'re not using ActiveRecord, or you\'d prefer not to run each of your\n # examples within a transaction, remove the following line or assign false\n # instead of true.\n config.use_transactional_fixtures = true\n\n # If true, the base class of anonymous controllers will be inferred\n # automatically. This will be the default behavior in future versions of\n # rspec-rails.\n config.infer_base_class_for_anonymous_controllers = false\n\n # Run specs in random order to surface order dependencies. If you find an\n # order dependency and want to debug it, you can fix the order by providing\n # the seed, which is printed after each run.\n # --seed 1234\n config.order = "random"\n\n config.include Devise::TestHelpers, :type => :controller\nend\n\nclass ActionController::TestCase\n include Devise::TestHelpers\nend\n
Run Code Online (Sandbox Code Playgroud)\n
我在将 Rails 4.2 和 Ruby 2.2 应用程序升级到 Rails 4.2 和 Rails 2.6 时遇到了这个问题。显然,ActionController::TestResponse
它被重用,因此被初始化多次。您可以添加阅读此线程的解决方法,或者简单地创建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
Run Code Online (Sandbox Code Playgroud)