Factory Girl 与 User.create——有什么区别?
这是对问题“工厂女孩 - 目的是什么?”的补充
说明我不确定我的问题是否算重复问题,但读完那篇文章后我仍然不太清楚,因为我认为我的疑问仍然不同。
好吧,现在我们总是可以在 Rspec 测试中的 before(:each) 块中使用 User.create() ,为什么我们还要费心使用 Factory Girl 呢?
当我阅读 Michael Hartl 的“Rails 3 教程”时,我突然陷入了 Factory Girl 的困惑,但在此之前一直使用 User.create() 构建了一个 User 实例。
我希望有人能为我澄清这一点,非常感谢!
This is an additional note to the question "Factory Girl - what's the purpose?"
I'm not sure whether my question is counted as a repetitive one, but I'm simply still not very clear after reading that post, since I think my doubt is still different.
Alright, so now that we can always use User.create() in before(:each) block in a Rspec test, why do we still bother using Factory Girl then?
This whole confuse occurred to me when I'm reading Michael Hartl's "Rails 3 tutorial", when he suddenly jumped into Factory Girl but used User.create() to build a User instance all the way through before that.
I wish someone can clarify this point for me, thx a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是对的,但是当您需要测试由于唯一性要求而需要不同属性值的大量数据时,像工厂女孩和制造这样的宝石可以显着减少跨类和不同测试中的代码重复。它混合了多种方法,可以轻松地为这些类型的测试“制造”许多不同的对象。
You are correct but gems like factory girl and fabrication allow for significantly less code duplication across clases and within different tests when you need to test large sets of data that require different attribute values due to uniqueness requirements. It mixes in methods to easily "manufacture" many different objects for these types of tests.