`autodetect':未检测到已知的ORM

时间:2015-05-11 13:03:20

标签: ruby-on-rails ruby ruby-on-rails-3 capybara

无法使用database_cleaner.rb清除数据;在运行测试时抛出以下问题。

  

/Users/prashanth_sams/.rvm/gems/ruby-2.0.0-p598/gems/database_cleaner-1.3.0/lib/database_cleaner/base.rb:147:in   `autodetect':未检测到已知的ORM!是ActiveRecord,DataMapper,   续集,MongoMapper,Mongoid,轻便摩托车或CouchPotato,Redis或Ohm   装? (DatabaseCleaner :: NoORMDetected)

enter image description here

spec_helper.rb

ENV["RAILS_ENV"] ||= 'test'

require File.expand_path("../config/environment", __FILE__)
require 'rspec/rails'

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

RSpec.configure do |config|

  config.mock_with :rspec

  config.use_transactional_fixtures = false


  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.color = true

  Selenium::Application.reload_routes!

end

database_cleaner.rb

require 'database_cleaner'

DatabaseCleaner.strategy = :truncation

RSpec.configure do |config|
  config.use_transactional_fixtures = false
  config.before :each do
    DatabaseCleaner.start
  end
  config.after :each do
    DatabaseCleaner.clean
  end
end

3 个答案:

答案 0 :(得分:5)

我在controller_spec上遇到了同样的问题。 'autodetect': No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded? (DatabaseCleaner::NoORMDetected)

我通过在控制器规范上要求rails_helper文件来解决。

require 'rails_helper'

在rails_helper.rb中需要文件'database_cleaner'。

require 'database_cleaner'

答案 1 :(得分:0)

使用我的设置,似乎适用于RDBMS(在MySQL和Postgres上检查),将其放入database_cleaner.rb

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  # this may be needed for Capybara tests and for testing after_commit hooks
  config.before(:each, strategy: :truncation) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end

如果您想使用truncation策略,请使用 describe 'something', strategy: :truncationit 'something', strategy: truncation

答案 2 :(得分:0)

我有这个问题(在Rails 5.1上),原因是我有

config.before(:suite) do
  DatabaseCleaner.clean_with :truncation
end

config.before(:each) do
  DatabaseCleaner.strategy = :transaction
end

config.before(:each) do
  DatabaseCleaner.start
end

config.after(:each) do
  DatabaseCleaner.clean
end

在我的spec_helper.rb中。

我也将require 'database_cleaner放入spec_helper.rb

所以最终我把这两件事都移到了rails_helper.rb,这解决了我的问题。