开发者

Is there a configuration setting to globally set the :default_strategy to :build for all factory_girl Factories?

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 previo开发者_StackOverflow中文版us 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.


From the source:

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

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-patch FactoryGirl::Factory#default_strategy.


FactoryGirl.use_parent_strategy

Follow https://github.com/thoughtbot/factory_girl/pull/961 for details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜