【问题标题】:Is there a configuration setting to globally set the :default_strategy to :build for all factory_girl Factories?是否有配置设置可以将所有 factory_girl 工厂的 :default_strategy 全局设置为 :build ?
【发布时间】:2010-11-29 09:35:34
【问题描述】:

我知道您可以像这样覆盖创建工厂对象的默认策略:

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

【问题讨论】:

    标签: ruby-on-rails ruby rspec factory-bot


    【解决方案1】:

    来自source

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

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

    【讨论】:

    • 我会试试这个。我想我应该向 factory_girl 提交代码补丁。
    【解决方案2】:

    FactoryGirl.use_parent_strategy

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

    【讨论】:

      猜你喜欢
      • 2017-09-04
      • 2013-06-29
      • 1970-01-01
      • 2011-02-10
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多