Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在factory girl中,您在哪里保存自定义保存策略?_Ruby On Rails_Json_Factory Bot - Fatal编程技术网

Ruby on rails 在factory girl中,您在哪里保存自定义保存策略?

Ruby on rails 在factory girl中,您在哪里保存自定义保存策略?,ruby-on-rails,json,factory-bot,Ruby On Rails,Json,Factory Bot,我正在尝试从FactoryGirl入门指南中实施新的FactoryGirl自定义构建策略。我想使用他们必须能够将我的工厂输出为JSON文件的代码 他们提供此代码以JSON格式输出: class JsonStrategy def initialize @strategy = FactoryGirl.strategy_by_name(:create).new end delegate :association, to: :@strategy def result(eval

我正在尝试从
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
中使用此代码,我应该将其保存到哪个目录

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


查看页面下方标题“注册自定义构建策略”关于
2/3

我自己刚刚遇到这个问题,一直在搜索保存新策略的位置

我似乎找不到一个自动加载/包含这些文件的地方,但您可以将文件保存到任何您喜欢的地方。我在lib/factory_girl/strategy/json.rb中保存了我的文件(这与在factory girl repo中保存的相同)

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

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

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

在那之后,使用和他们的指南中描述的一样,你应该可以开始了。

我的自定义策略保存在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中为它们创建一个目录。我在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

我认为这是一条路,我同意把它放在spec/而不是lib/中更有意义。