3

我正在尝试从入门指南中实施新的FactoryGirl自定义构建策略。FactoryGirl我想使用他们必须能够将我的工厂输出为JSON文件的代码。

他们提供此代码以输出JSON

class JsonStrategy
  def initialize
    @strategy = FactoryGirl.strategy_by_name(:create).new
  end

  delegate :association, to: :@strategy

  def result(evaluation)
    @strategy.result(evaluation).to_json
  end
end

我应该在哪个目录保存此代码以便在其中使用它FactoryGirl

这是我获取代码的链接以及对其功能的进一步解释:

http://robots.thoughtbot.com/post/21719164760/factorygirl-3-2-so-awesome-it-needs-to-be-released

在页面下方的标题"REGISTER CUSTOM BUILD STRATEGIES"2/3查看。

4

2 回答 2

3

我的自定义策略保存在 spec/support/custom_strategies.rb

#spec/support/custom_strategies.rb  
class JsonStrategy                                                                                                                                                                                                                                                            
  def initialize                                                                                                                                                                                                                                                              
    @strategy = FactoryGirl.strategy_by_name(:create).new                                                                                                                                                                                                                     
  end                                                                                                                                                                                                                                                                         

  delegate :association, to: :@strategy                                                                                                                                                                                                                                       

  def result(evaluation)                                                                                                                                                                                                                                                      
    @strategy.result(evaluation).to_json                                                                                                                                                                                                                                      
  end                                                                                                                                                                                                                                                                         
end    

class SecondCustomStrategy
end

或者,如果您想将它们分开,您可以在 spec/support 中为 custom_strategies 创建一个目录。我在 spec/spec_helper.rb 中注册了自定义策略

#spec/spec_helper.rb
  RSpec.configure do |config|
    ##some code

    config.order = "random"                                                                                                                                                                                                                                                     

    config.filter_run_excluding :broken => true                                                                                                                                                                                                                                 

    config.include FactoryGirl::Syntax::Methods                                                                                                                                                                                                                                                                                                                                                                                                                                               
  end                                 

  #registered after the Rspec.configure block                                                                                                                                                                                                                                                                                  
  FactoryGirl.register_strategy(:json, JsonStrategy)                                                                                                                                                                                                                            

  require 'capybara/poltergeist'                                                                                                                                                                                                                                                
  Capybara.javascript_driver = :poltergeist
于 2014-03-04T19:04:10.423 回答
2

我自己也遇到了这个问题,一直在寻找保存新策略的地方。

我似乎找不到自动加载/包含它们的地方,但是您可以将文件保存在任何您喜欢的地方。我将我的保存在 lib/factory_girl/strategy/json.rb 中(这与它们保存在 factory girl 存储库中的约定相同)。

保存后,您需要按照指南中的说明注册新策略。如果您的策略不在您的加载路径中(我的不在),那么您需要在注册之前要求它。

这是我在 spec_helper.rb 文件中的代码,用于要求然后注册策略

require "#{Rails.root}/lib/factory_girl/strategy/json"
FactoryGirl.register_strategy(:json, JsonStrategy)

之后,使用就像他们的指南中描述的那样,你应该很高兴。

于 2013-09-12T22:08:58.143 回答