我们应该在Rails Factory中使用Faker吗? [英] Should we be using Faker in Rails Factories?

查看:111
本文介绍了我们应该在Rails Factory中使用Faker吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢 Faker ,我一直在seeds.rb中使用它来填充我的开发环境带有真实的数据.

我也刚开始使用 Factory Girl ,它也节省了很多时间-但是当我在网络上搜索代码示例,我看不出有人将两者结合在一起的证据.

问人们在工厂中不使用伪造者有充分的理由吗?

我的感觉是,这样做可以通过每次都随机(但可预测)种子数据来提高测试的健壮性,希望可以增加弹出错误的可能性.

但是也许这是不正确的,或者与对工厂进行硬编码没有任何好处,或者我没有看到潜在的陷阱.是否有充分的理由将这两个宝石合并或不合并?

解决方案

有人反对它,例如此处.

请勿使用随机属性值

一种常见的模式是使用伪造的数据库(例如Faker或Forgery)在 苍蝇.这对于名称,电子邮件地址或 电话号码,但没有实际用途.创造独特 值对于序列很简单:

FactoryGirl.define do   
  sequence(:title) { |n| "Example title #{n}" }

  factory :post do
    title
  end 
end

FactoryGirl.create(:post).title # => 'Example title 1' 

您的随机分组 数据可能会在某些时候触发测试中的意外结果, 使您的工厂无所适从.任何可能的值 必须以某种方式影响您的测试结果, 含义:

随着时间的流逝,您会发现导致测试失败的新属性 有时会失败.这是一个令人沮丧的过程,因为测试可能会失败 每十或一百次运行仅一次-取决于有多少次 属性和可能的​​值,以及哪种组合 触发错误.您将必须在中列出所有此类随机属性 每次测试都将其覆盖,这很愚蠢.因此,您创建了非随机 工厂,从而否定了原始随机性的任何好处. 就像亨里克·尼(Henrik Nyh)所做的那样,有人可能会争辩说随机值可以帮助您 发现错误.虽然可能,但这显然意味着您拥有更大的 问题:测试套件中存在漏洞.在最坏的情况下,该错误 仍然未被发现;在最好的情况下,您会感到神秘 错误消息,在您下次运行测试时消失, 很难调试.诚然,一个隐秘的错误总比没有错误要好,但是 随机工厂仍然不能替代适当的单元测试, 代码审查和TDD,以防止出现这些问题.

因此,随机化的工厂不仅不值得付出努力,而且值得 甚至给您对测试的错误信心,这比 根本没有测试.

但是,只要您愿意,没有什么可以阻止您这样做.

哦,在最近的FactoryGirl中有一种更简单的方法来内联序列,该引用是为较旧的版本编写的.

I love Faker, I use it in my seeds.rb all the time to populate my dev environment with real-ish looking data.

I've also just started using Factory Girl which also saves a lot of time - but when i sleuth around the web for code examples I don't see much evidence of people combining the two.

Q. Is there a good reason why people don't use faker in a factory?

My feeling is that by doing so I'd increase the robustness of my tests by seeding random - but predictable - data each time, which hopefully would increase the chances of a bug popping up.

But perhaps that's incorrect and there is either no benefit over hard coding a factory or I'm not seeing a potential pitfall. Is there a good reason why these two gems should or shouldn't be combined?

解决方案

Some people argue against it, as here.

DO NOT USE RANDOM ATTRIBUTE VALUES

One common pattern is to use a fake data library (like Faker or Forgery) to generate random values on the fly. This may seem attractive for names, email addresses or telephone numbers, but it serves no real purpose. Creating unique values is simple enough with sequences:

FactoryGirl.define do   
  sequence(:title) { |n| "Example title #{n}" }

  factory :post do
    title
  end 
end

FactoryGirl.create(:post).title # => 'Example title 1' 

Your randomised data might at some stage trigger unexpected results in your tests, making your factories frustrating to work with. Any value that might affect your test outcome in some way would have to be overridden, meaning:

Over time, you will discover new attributes that cause your test to fail sometimes. This is a frustrating process, since tests might fail only once in every ten or hundred runs – depending on how many attributes and possible values there are, and which combination triggers the bug. You will have to list every such random attribute in every test to override it, which is silly. So, you create non-random factories, thereby negating any benefit of the original randomness. One might argue, as Henrik Nyh does, that random values help you discover bugs. While possible, that obviously means you have a bigger problem: holes in your test suite. In the worst case scenario the bug still goes undetected; in the best case scenario you get a cryptic error message that disappears the next time you run the test, making it hard to debug. True, a cryptic error is better than no error, but randomised factories remain a poor substitute for proper unit tests, code review and TDD to prevent these problems.

Randomised factories are therefore not only not worth the effort, they even give you false confidence in your tests, which is worse than having no tests at all.

But there's nothing stopping you from doing it if you want to, just do it.

Oh, and there is an even easier way to inline a sequence in recent FactoryGirl, that quote was written for an older version.

这篇关于我们应该在Rails Factory中使用Faker吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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