4

我知道您可以覆盖创建工厂对象的默认策略,如下所示:

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对象,而不是默认创建一个对象。

4

2 回答 2

0

来源

module FactoryGirl
  class Factory
    # ...
    def default_strategy #:nodoc:
      @options[:default_strategy] || :create
    end
    # ...
  end
end

默认策略等于作为选项传递给定义的策略,否则设置为:create。因此,除非您使用猴子补丁,否则似乎不可能为所有工厂设置策略FactoryGirl::Factory#default_strategy

于 2010-11-29T21:03:11.360 回答
0

FactoryGirl.use_parent_strategy

关注https://github.com/thoughtbot/factory_girl/pull/961了解详情。

于 2017-08-25T19:02:03.183 回答