ruby-on-rails-3 – Spork,RSpec和database_cleaner销毁开发数据
发布时间:2020-12-16 19:12:53 所属栏目:百科 来源:网络整理
导读:我的Rails 3.1应用程序中有以下spec_helper.rb文件.我正在使用Spork加载环境以便进行测试.在将Spork添加到混合物之前,我的所有测试都有效.添加spork之后,测试数据库在测试运行之间没有得到适当的清除,这导致了我的一些期望. 按照其他说明,我将database_clean
我的Rails 3.1应用程序中有以下spec_helper.rb文件.我正在使用Spork加载环境以便进行测试.在将Spork添加到混合物之前,我的所有测试都有效.添加spork之后,测试数据库在测试运行之间没有得到适当的清除,这导致了我的一些期望.
按照其他说明,我将database_cleaner添加到下面列出的代码中;但是,现在,开发数据库以及测试数据库正在被清理. ENV [“RAILS_ENV”]呼叫在此呼叫期间返回测试. 有没有办法明确限制对DatabaseCleaner.clean_with(:truncation)的调用只影响测试数据库? require 'rubygems' require 'spork' Spork.prefork do ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment",__FILE__) require 'rspec/rails' require 'shoulda/matchers/integrations/rspec' require 'database_cleaner' Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} RSpec.configure do |config| config.mock_with :mocha config.formatter = 'documentation' config.use_transactional_fixtures = true config.before(:suite) do DatabaseCleaner.strategy = :truncation end config.before(:each) do DatabaseCleaner.start end config.after(:each) do DatabaseCleaner.clean end end end Spork.each_run do FactoryGirl.reload end 更新:这是我的database.yml文件 development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000 test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000 此外,我通过将clean_with调用移动到before(:each)部分来解决基本问题,但这会显着减慢测试运行速度. 解决方法
您是否尝试将ENV [“RAILS_ENV”] || =’test’移出Spork.prefork块?
您确定Spork导致您的数据库未清理吗?如果您正在使用RSpec的事务性工具,那么唯一可能导致这种情况的是在before(:all)块中使用工厂.您可以在after(:all)块中清理数据并删除DatabaseCleaner. 顺便说一句,如果您使用截断策略,则无需运行DatabaseCleaner.start. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |