Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby-Rspec自动化中数据库清理器gem的集成问题_Ruby On Rails_Ruby_Database_Rspec - Fatal编程技术网

Ruby on rails Ruby-Rspec自动化中数据库清理器gem的集成问题

Ruby on rails Ruby-Rspec自动化中数据库清理器gem的集成问题,ruby-on-rails,ruby,database,rspec,Ruby On Rails,Ruby,Database,Rspec,我在执行代码时遇到以下错误 未检测到已知ORM!是否加载了ActiveRecord、DataMapper、Sequel、MongoMapper、Mongoid、Moped或Couch马铃薯、Redis或Ohm?(DatabaseCleaner::NoORMDetected) 谁能为这个问题提出一个解决方案 spec\u helper.rb: ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment

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

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

谁能为这个问题提出一个解决方案

spec\u 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

将以下内容添加到
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

有关更多信息,请参阅。

希望这有帮助。以下是如何配置(工作)数据库清理器:

  • 我没有
    数据库\u cleaner.rb
    。我在
    rails\u helper.rb
    中配置所有内容。这应该无关紧要,但您可能希望尝试下面的代码,看看它是否工作,然后再将其拆分

  • 请注意,我在
    块之前的
    中有
    DatabaseCleaner.strategy=:truncation

  • 下面是我的
    rails\u 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
    

  • 你好谢谢你的帮助。我已经在spec_helper.rb中添加了上面给定的代码Rspec.configure块;然而,面对同样的问题。请帮助我。我已经在上面展示了spec_helper.rb和database_cleaner.rb。请看上面。你定义了一个模型吗?