【问题标题】:How to populate and clean a database in ruby on rails with a rake task and database cleaner gem?如何使用 rake 任务和数据库清理器 gem 在 ruby​​ on rails 中填充和清理数据库?
【发布时间】:2015-12-31 01:29:05
【问题描述】:

我在一个文件中有两个 rake 任务。播种和填充。 seed 正在创建必要的数据,populate 正在为我的测试填充样本数据。

在我的测试中,我正在操作一个数据库(添加新条目、删除它等等)。我正在使用数据库清洁器 gem 来重置数据库但是当我重置数据库时,我需要为下一次测试填充数据。我有以下设置但它不起作用数据库仍然是空的并且填充数据不存在。

这是规范/支持/database_cleaner.rb

require 'rake'
load File.expand_path("../../../lib/tasks/my_tasks.rake", __FILE__)
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
  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
    Rake::Task.define_task(:environment)
    Rake::Task['db:seed'].invoke
    Rake::Task['db:populate'].invoke
  end

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

spec/rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'rake'

require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
 require 'spec_helper'
 require 'factory_girl_rails'
 require 'rspec/rails'
 require 'capybara/poltergeist'

 # Checks for pending migrations before tests are run.
 # If you are not using ActiveRecord, you can remove this line.
 ActiveRecord::Migration.maintain_test_schema!

 Capybara.configure do |config|
   config.default_wait_time = 20
 end

 RSpec.configure do |config|
 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
 #
 #  config.fixture_path = "#{::Rails.root}/spec/fixtures"

 config.include FactoryGirl::Syntax::Methods

 # If you're not using ActiveRecord, or you'd prefer not to run each of your 
 # examples within a transaction, remove the following line or assign false
 # instead of true.

 config.infer_spec_type_from_file_location!
end

【问题讨论】:

  • 我的警卫日志中没有任何错误。如果我的方法是错误的,我可以改变它。

标签: ruby-on-rails ruby rake rake-task database-cleaner


【解决方案1】:

我找到了解决方法,但这并不完美,我仍在寻找解决方案,所以如果你找到了,请发布答案。我不会选择这个作为正确答案。

您需要将内容 spec/support/database_cleaner.rb 文件放入 spec/spec_helper.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

  config.before(:each, :js => true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
    Rake::Task.define_task(:environment)
    Rake::Task['db:seed'].invoke
    Rake::Task['db:populate'].invoke
  end

  config.after(:each) do
    # DatabaseCleaner.clean
  end
  config.infer_spec_type_from_file_location!
end

重要提示:您需要评论或删除:DatabaseCleaner.clean

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多