2015-05-11 42 views
9

無法使用database_cleaner.rb清除數據;在運行測試時拋出以下問題。`autodetect':未檢測到已知的ORM

/Users/prashanth_sams/.rvm/gems/ruby-2.0.0-p598/gems/database_cleaner-1.3.0/lib/database_cleaner/base.rb:147:in '自動檢測':沒有已知檢測到ORM! ActiveRecord,DataMapper, Sequel,MongoMapper,Mongoid,Moped或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 
+0

結帳這個博客來配置數據庫清潔器http://devblog.avdi.org/2012/08/31/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/ –

+0

您是否定義了一個模型? – gnerkus

回答

0

用我的設置,似乎工作罰款RDBMS(che cked在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

4

我在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' 
0

我有這個問題(on Rails的5.1),原因是我在spec_helper.rb

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 

我把require 'database_cleaner也放在spec_helper.rb之內。

因此,最終我將兩個東西都搬到了rails_helper.rb,這解決了我的問題。