user3382097
user3382097

Reputation: 35

Issue on integrating Database cleaner gem in Ruby Rspec automation

I am getting the following error on executing the code.

No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded? (DatabaseCleaner::NoORMDetected)

Can anyone pls suggest a solution for this.

spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'

require 'database_cleaner'
DatabaseCleaner.strategy = :truncation

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/database_cleaner.rb")].each {|f| require f}

RSpec.configure do |config|
  config.mock_with :mocha

  config.before(:each) do
    DatabaseCleaner.clean
    #Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop) # transactional fixtures hack for mongo
  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

  config.use_transactional_fixtures = false
end

Upvotes: 3

Views: 1049

Answers (2)

madcow
madcow

Reputation: 2633

Hope this helps. Here is how I have my (working) database cleaner configured:

  1. I have no database_cleaner.rb. I configure everything in rails_helper.rb. This shouldn't matter, but you may want to try below code and see if it works before splitting it out.

  2. Notice that I have DatabaseCleaner.strategy = :truncation in a before block

  3. Here is the relevant part of my rails_helper.rb

    RSpec.configure do |config|
      config.use_transactional_fixtures = false
    
      config.before(:suite) do
        DatabaseCleaner.clean_with :truncation
      end
    
      config.before(:each) do |example|
        if example.metadata[:js]
          DatabaseCleaner.strategy = :truncation
        else
          DatabaseCleaner.strategy = :transaction
        end
        DatabaseCleaner.start
      end
    
      config.after(:each) do
        DatabaseCleaner.clean
      end
    end
    

Upvotes: 3

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34318

Add the following to your RSpec.configure block:

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

Also, to specify particular ORM, you can do this:

#How to specify particular orms
DatabaseCleaner[:active_record].strategy = :transaction
DatabaseCleaner[:mongo_mapper].strategy = :truncation

See this for more information.

Upvotes: 1

Related Questions