我应该如何处理用于测试目的的工厂用途?
我正在使用 Ruby on Rails 3.0.9、RSpect 2 和 FactoryGirl。我想知道在我的案例中,我应该如何处理与工厂用途相关的测试目的。
我已经在数据库中播种了一些数据(通过使用 /db/seed.rb
文件),这些数据是初始化使我的应用程序正常工作所必需的。现在我遇到了麻烦,因为我实现\创建了一些工厂,并且当我实例化它们时(例如:Factory(:user)
、Factory(:user_authorization)
,...)这些数据与数据库中存在的种子数据“组合”。
所以,问题是:我应该独占使用工厂(即仅使用工厂数据而不考虑测试数据库数据)或者我可以使用那些与数据库中的种子数据“组合”? 也就是说,我是否还应该考虑数据库种子数据,或者我应该使用工厂来实现\模拟所有种子数据?
I am using Ruby on Rails 3.0.9, RSpect 2 and FactoryGirl. I would like to know how I should proceed in my case relating to factory usages for testing purposes.
I have seeded the database (by using the /db/seed.rb
file) with some data that is necessary in order to initialize a make my application to work. Now I am in trouble because I implemented\created some factories and when I instantiate them (eg: Factory(:user)
, Factory(:user_authorization)
, ...) those are "combined" with the seeded data present in the database.
So, the question is: should I use factories exclusively (that is, to use only factory data by not considering the test database data) or can I use those in "combination" with the seeded data in the database? That is, should I consider also database seeded data or I should implement\emulate all seeded data with factories?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我的项目中,我使用播种来获取应用程序启动时真正需要的数据。例如,第一个用户(管理员)、域表……对于这些我不使用工厂。
其次,我在测试期间使用工厂来模拟应该存在的任何数据。
在我看来,这些完美地共存。当数据的内容实际上并不那么重要(必须有一些用户,一些帖子,一些评论......)时,我使用工厂,但是通过播种,数据库中的实际数据非常重要,如果我为此,我将使用工厂,无论如何我都会重新定义每个属性,所以对我来说使用工厂没有任何好处。
我希望这有帮助。
In my projects I use seeding for the data my application really needs to get started. E.g. the first user (admin), domain tables, ... For these I do not use factories.
Secondly I use factories during testing to simulate any data that should be present.
In my opinion those co-exist perfectly. I use the factories when the contents of the data is actually not that important (there has to be some user, some post, some comment ...), but with the seeding the actual data in the database is very important, and if I would be using factories for that, I would be redefining every attribute anyway, so for me there would be no benefit of using a factory.
I hope this helps.
您可以在seed.rb 中使用factory_girl。这篇文章对此进行了更多解释。 http://xtargets.com/2011/ 02/06/播种你的rails-db-with-factory-girl/
You can use factory_girl within in seed.rb. This post explains it a little more. http://xtargets.com/2011/02/06/seeding-your-rails-db-with-factory-girl/