有没有办法从现有的一组模型生成 Rails 夹具?

发布于 2024-11-06 19:24:39 字数 292 浏览 6 评论 0原文

我有一个没有测试的 Rails 2.x 应用程序。我可以手动编写测试,但是有没有办法自动生成装置?如果不必手动输入所有内容,那就太好了。

我可以为所有模型再次运行脚本/生成,但一切都已经存在,如果我正确理解生成器,我仍然需要输入所有属性。

我考虑过运行 Rails 控制台并执行以下操作...

>> y VendorUser.all.rand

这会给我一些包含所有属性的 YAML,但它们会乱序,而且仍然非常耗时。

谁能建议一个更有效的选择?

I have a Rails 2.x app with no tests. I can write out the tests manually, but is there a way to generate fixtures automatically? It would be nice to not have to type all that out by hand.

I could run script/generate again for all the models, but everything already exists and if I understand generators correctly, I'd still have to type in all the attributes.

I thought about running the Rails console and doing for example...

>> y VendorUser.all.rand

That would give me some YAML with all the attributes, but they'd be out of order and it's still pretty time-consuming.

Can anyone suggest a more efficient option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌倦 2024-11-13 19:24:39

这是一个生成灯具的 rake 任务。

desc "extracting data for fixtures"
task :extract_fixtures => :environment do
  sql  = "SELECT * FROM %s"
  skip_tables = ["schema_info","schema_migrations"]
  ActiveRecord::Base.establish_connection
  (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
    i = "000"
    File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w' ) do |file|
      data = ActiveRecord::Base.connection.select_all(sql % table_name)
      file.write data.inject({}) { |hash, record|
        hash["#{table_name}_#{i.succ!}"] = record
        hash
      }.to_yaml
    end
  end
end

Here is a rake task to generate fixtures.

desc "extracting data for fixtures"
task :extract_fixtures => :environment do
  sql  = "SELECT * FROM %s"
  skip_tables = ["schema_info","schema_migrations"]
  ActiveRecord::Base.establish_connection
  (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
    i = "000"
    File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w' ) do |file|
      data = ActiveRecord::Base.connection.select_all(sql % table_name)
      file.write data.inject({}) { |hash, record|
        hash["#{table_name}_#{i.succ!}"] = record
        hash
      }.to_yaml
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文