在测试我的控制器时,我遇到了这个问题。 我正在使用 rspec-rails-3.4.2、rails-4.2.0 和 mongodb。 这是我的 rails_helper.rb
`ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/core'
require 'rspec/rails/extensions'
require 'rspec/rails/view_rendering'
require 'rspec/rails/adapters'
require 'rspec/rails/matchers'
require 'rspec/rails/fixture_support'
require 'rspec/rails/example'
require 'rspec/rails/vendor/capybara'
require 'mongoid-rspec'
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.orm = "mongoid"
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
请只要求 rspec-rails 作为require 'rspec/rails'
这很可能是你的问题的原因
@JonRowe感谢您的即时回复。 我也试过。 当我需要 respec-rails 时,它会导致另一个错误
失败/错误:引发 ConnectionNotEstablished,“#{klass} 没有连接池”,除非池
是的,这是您的配置错误问题,您需要确保在 rspec-rails 之前加载了 mongoid,因此它不会尝试使用 ActiveRecord。
在尝试 RSpec 之前,请确保您可以无误地执行RAILS_ENV=test rails console
。
为了让我们进一步提供帮助,您必须提供回溯(通过https://gist.github.com
)
最有用的评论
请只要求 rspec-rails 作为
require 'rspec/rails'
这很可能是你的问题的原因