在我的单元测试中,固定装置和工厂有什么区别?
这里有点困惑,工厂和夹具有什么区别?
所以我正在使用factory_girl,当我使用工厂创建对象时,是否应该将其持久化到数据库?或者这就是固定装置,数据通过模型等持久保存到数据库。
或者这些完全不相关:)
A little confused here, what is the difference between a factory and a fixture?
So I'm using factory_girl, when I create an object using the factory, should it be persisted to the db? Or is that what a fixture is, data persisted to the db via the models etc.
Or are these totally unrelated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
真正简短的答案是,Rails 夹具和 Factory Girl 工厂在概念上没有区别。这只是完成同一件事的不同语法。
然而,这会让人感到困惑,因为一般测试/编码概念的术语已被 Rails/Ruby 世界中的各个项目所采用。一般来说:
基本上,在 Rails 中,Fixtures 和 Factories(由 Factory Girl 定义)都只是创建对象的方式 您应该使用它们来创建您的测试夹具,或者测试的设置状态。令人困惑的部分是,您可以使用多个 Rails“Fixtures”来创建单个测试的 Fixture(基线)。
Factory Girl 本身是 Object Mother 模式的实现,它描述了一种创建测试夹具设置的方法使用角色。
The really short answer is that there is no difference conceptually between a Rails fixture and a Factory Girl factory. This is just different syntax to accomplish the same thing.
However, this gets confusing because the terminology of general testing/coding concepts has been co-opted by individual projects in the Rails/Ruby world. In general:
Basically in Rails, both Fixtures and Factories (as defined by Factory Girl), are just ways of creating objects. You are supposed to use them to create your Test Fixture, or the setup state of your test. The confusing part is that you can use multiple Rails "Fixtures" to create the Fixture (baseline) of an individual test.
Factory Girl itself is an implementation of the Object Mother pattern, that describes a way of creating test fixture setup using personas.
它们用于实现相同的目标,即用测试数据填充数据库。
夹具比工厂快得多,因为它们不使用验证或任何模型逻辑,但很难维护,并且缺乏验证可能会破坏您的测试。
通过工厂,您可以在数据库中获得有意义(且有效)的内容,但代价是速度。
还有其他解决方案,例如fixture_builder,可以让您使用工厂定义固定装置;)工厂仅运行一次,然后转换为固定装置。 https://github.com/rdy/fixture_builder
Well they are used to achieve the same goal, to populate the DB with data for your test.
Fixtures are way faster than factories because they don't use validation or any of the model's logic but are very hard to maintain and the lack of validation can break your tests.
With factories you have meaningful (and valid) content in the DB at the cost of speed.
There are other solutions though like fixture_builder that let you define the fixtures using factories ;) the factories are run just once and then converted to fixtures. https://github.com/rdy/fixture_builder
工厂(例如使用factory_girl)只是使用预定义值构建对象的一种方法。它不需要您将它们插入任何类型的持久存储中。只是构建复杂的对象很方便。显然,factory_girl 有更多的功能,但在我看来,这是它的主要目标。
注意:我在这里只回答一半的问题,因为我对装置不是很熟悉......
只是把我的 2 美分扔在这里。希望这有帮助。
A Factory, using factory_girl for instance, is only a mean to build objects with predefined values. It doesn't require your to insert them into any kind of persisted store. It is just convenient to build complex objects. factory_girl has obviously many more features, but that is its primary goal as I see it.
NOTE : I'm answering only half of the question here because I'm not very familiar with the fixture...
just throwing my 2 cents here. Hope this helps.