简体   繁体   English

在> = Rails 3.2.0中创建“测试”数据库内容的最佳方法是什么?

[英]What's the best method for creating “test” database content in >= Rails 3.2.0?

I want to be able to create a few dozen users, articles (or whatever resources are unique to the app), etc to see how the app looks and responds when full. 我希望能够创建几十个用户,文章(或该应用程序独有的任何资源)等,以查看该应用程序的外观以及填充后的响应方式。 This is just for testing/dev purposes, so I want to be able to roll it back, destroy it, or whatever easily. 这只是出于测试/开发目的,因此我希望能够轻松地将其回滚,销毁或进行其他操作。 Perhaps I'm overthinking it, who knows. 谁知道呢,也许我想得太多了。

I've seen people recommend just using a standard migration, which is one idea, but I want to do this OPTIONALLY, I don't want everyone on the project to get the sample content as they update the app. 我见过有人建议仅使用标准迁移,这是一个主意,但我想这样做是可选的,我不希望项目中的每个人都在更新应用程序时获得示例内容。

Other people have mentioned Factory Girl, but that looks like it might be either overkill or a side-use of a gem really designed for testing, etc. It wasn't perfectly clear. 其他人提到过“工厂女郎”,但看起来它要么是矫kill过正,要么是真正为测试目的而设计的宝石的侧面使用等。目前还不是很清楚。

So what do you all do in this case? 那么你们在这种情况下会做什么?

I recommend a rake task. 我推荐一个耙任务。 You can stick it in lib/tasks and so everyone in the project gets it, but not everyone needs to run it, and only when it's run will it do anything. 您可以将其粘贴在lib/tasks ,以便项目中的每个人都可以使用它,但并不是每个人都需要运行它,只有在运行时它才能执行任何操作。 This a great tutorial on writing rake tasks, just remember to read the part under the Rails heading in order to learn how to bring in your models. 这是一篇有关编写rake任务的出色教程 ,只记得记住阅读Rails标题下的部分,以了解如何引入模型。

After that, your rake tasks is basically just ruby code. 在那之后,您的瑞克任务基本上只是红宝石代码。 I'd suggest using the dynamic find_or_create_by methods in order to explicitly create the models you want, and if it's run multiple times, they won't be created multiple times. 我建议使用动态find_or_create_by方法来显式创建所需的模型,并且如果该模型多次运行,则不会多次创建它们。 You can also choose to destroy all records in a particular model before creating them. 您还可以选择在创建特定模型之前销毁所有记录。

I wouldn't recommend using Factory Girl because you probably want explicit control over how your models are created. 我不建议使用Factory Girl,因为您可能希望显式控制模型的创建方式。

Here's an example rake task to show how easy it is: 这是一个示例性的rake任务,展示了它的简单程度:

#lib/tasks/my_task.rake
task :fake_data => :environment do
    MyModel.find_or_create_by_name("Test")
end

Then in your console: 然后在您的控制台中:

rake fake_data

Or: 要么:

rake fake_data RAILS_ENV=test

Ta da! da!

Take a look at Rails seed data features 看一下Rails种子数据功能

http://railscasts.com/episodes/179-seed-data http://railscasts.com/episodes/179-seed-data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 创建1个页面以在Rails中请求数据库交互的最佳方法是什么? - What's the best approach for creating 1 off pages that requeire database interaction in rails? Rails 3.2.0和3.2.7之间的文件区别是什么? (关于:升级) - What's the file difference between rails 3.2.0 and 3.2.7? (re: upgrading) 在Rails中A / B测试电子邮件主题行的最佳方法是什么? - What's the best way to A/B test Email Subject Lines in Rails? 用tdd / bdd在轨中测试Mongoid模型的最佳方法是什么? - What's the best way to test mongoid models in rails with tdd/bdd? 在Rails中自动删除用户帖子的最佳方法是什么? - What's the best method for automatically deleting users posts in Rails? Rails:lambda,scope和class方法有什么区别? 什么是最佳做法? - Rails: What's the difference between lambda, scope and class method? What's best practice? 在Rails 4中创建类别的最佳方法是什么? - What is the best way of creating categories in Rails 4? 在Rails中生成“ widgetized”内容的最佳实践是什么 - What are the best practices to generate “widgetized” content in Rails 什么是测试查询Rails性能的最佳方法 - What is the best way to test performance of the query Rails 测试rails应用程序的最佳方法是什么? - What is the best way to test a rails app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM