在Rails中重新生成夹具测试文件 [英] Regenerate fixture test files in Rails

查看:66
本文介绍了在Rails中重新生成夹具测试文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重新生成所有YML夹具文件?我不小心删除了它们.

How do I regenerate all the YML fixture files? I accidentally deleted them.

推荐答案

@brian,

我正在使用以下脚本从给定的sql生成固定装置

I'm using the following script to generate the fixtures from a given sql

这作为耙任务位于我的lib/task目录下

This is under my lib/task directory as a rake task

namespace :fixture_generator do
  desc "generate fixtures for a given sql query from the current development database"

  task :fixture_generator, [:sql, :file_name] => :environment do |t, args|
    args.with_defaults(:sql => nil, :file_name => nil)
    i = "000"
    p "creating fixture - #{args.file_name}"
    File.open("#{Rails.root}/test/fixtures/#{args.file_name}.yml", 'a+') do |file|
      data = ActiveRecord::Base.connection.select_all(args.sql)
      file.write data.inject({}) { |hash, record|
        number = i.succ!
        hash["#{args.file_name}_#{number}"] = record
        hash
      }.to_yaml
    end

  end
end

用法,说我想为用户表生成固定装置

Usage, Say I want to generate fixture for users table

rake fixture_generator:fixture_generator["select * from users","users"]

而且,如果您使用相同的fixture文件名运行另一个查询,它将附加到现有查询中

And also, If you run another query with the same fixture file name, it will append to the existing one

HTH

这篇关于在Rails中重新生成夹具测试文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆