【问题标题】:Issue on integrating Database cleaner gem in Ruby Rspec automation在 Ruby Rspec 自动化中集成数据库清洁器 gem 的问题
【发布时间】:2015-09-10 09:53:26
【问题描述】:

我在执行代码时收到以下错误。

未检测到已知的 ORM!是否加载了 ActiveRecord、DataMapper、Sequel、MongoMapper、Mongoid、Moped 或 CouchPotato、Redis 或 Ohm? (DatabaseCleaner::NoORMDetected)

任何人都可以为此提出解决方案。

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

【问题讨论】:

  • 你定义模型了吗?

标签: ruby-on-rails ruby database rspec


【解决方案1】:

希望这会有所帮助。这是我配置(工作)数据库清理器的方式:

  1. 我没有database_cleaner.rb。我在rails_helper.rb 中配置所有内容。这应该没关系,但您可能想尝试下面的代码并在拆分之前查看它是否有效。

  2. 请注意,我在 before 块中有 DatabaseCleaner.strategy = :truncation

  3. 这是我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
    

【讨论】:

    【解决方案2】:

    将以下内容添加到您的 RSpec.configure 块中:

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

    另外,要指定特定的 ORM,您可以这样做:

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

    更多信息请参见this

    【讨论】:

    • 嗨。感谢您的帮助。
    • 我在 spec_helper.rb 中添加了上面给出的代码 Rspec.configure 块;但是,面临同样的问题。请帮助我。
    • 我已经在上面展示了 spec_helper.rb 和 database_cleaner.rb。请看上面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多