rails: 执行rake test而不清除测试数据库中的数据

来源:互联网 发布:剑三捏脸数据成男鹿晗 编辑:程序博客网 时间:2024/06/04 23:25

以下技术应用于最优质的水果的鲜果篮

执行rake test,rails默认会做:

1, Remove any existing data from the table corresponding to the fixture
2, Load the fixture data into the table
3, Dump the fixture data into a variable in case you want to access it directly


为了避免清除测试数据库中的数据,需要:

1, 在lib/tasks文件夹下面创建新文件,以.rake为扩展名(例如,keep_data.rake),并加入以下代码。

Rake::TaskManager.class_eval do
  def delete_task(task_name)
    @tasks.delete(task_name.to_s)
  end
  Rake.application.delete_task("db:test:purge")
  Rake.application.delete_task("db:test:prepare")
end

namespace :db do
  namespace :test do
    task :purge do
    end
    task :prepare do
    end
  end
end


2, 在test/test_helper.rb中注释掉这行代码:fixtures :all