是否有一个配置设置可以将所有factory_girl工厂的:default_strategy全局设置为:build?
我知道您可以像这样覆盖创建 Factory 对象的默认策略:
Factory.define :person, :default_strategy => :build do
# stuff
end
Factory.define :person, :default_strategy => :create do
# stuff
end
# same behavior as the previous factory
Factory.define :person do
# stuff
end
但我想知道是否可以将设置添加到factory_girl 配置文件中或者在 /environments/test.rb
文件中这样
Factory.define :person do
# stuff
end
默认情况下会构建一个 Person
对象,而不是默认创建一个对象。
I know you can override the default strategy for creating a Factory object like so:
Factory.define :person, :default_strategy => :build do
# stuff
end
Factory.define :person, :default_strategy => :create do
# stuff
end
# same behavior as the previous factory
Factory.define :person do
# stuff
end
but I'm wondering if I can add a setting to a factory_girl config file or maybe in the /environments/test.rb
file so that
Factory.define :person do
# stuff
end
builds a Person
object by default and not create one by default.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 来源:
默认策略等于作为选项传递给定义,否则设置为
:create
。因此,除非您对FactoryGirl::Factory#default_strategy
进行猴子补丁,否则似乎不可能为所有工厂设置策略。From the source:
The default strategy equals the strategy that is passed as an option to the definition, and otherwise is set to
:create
. So it seems that's it not possible to set the strategy for all factories unless you monkey-patchFactoryGirl::Factory#default_strategy
.FactoryGirl.use_parent_strategy
关注 https://github.com/thoughtbot/factory_girl /pull/961 了解详细信息。
FactoryGirl.use_parent_strategy
Follow https://github.com/thoughtbot/factory_girl/pull/961 for details.