Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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 FactoryGirl在factory中更改属性结构_Ruby On Rails_Factory Bot - Fatal编程技术网

Ruby on rails FactoryGirl在factory中更改属性结构

Ruby on rails FactoryGirl在factory中更改属性结构,ruby-on-rails,factory-bot,Ruby On Rails,Factory Bot,工厂女孩4.5.0 如何更改已传递属性的结构?例如,在处理大量现有测试时,在序列化现有字段时,希望工厂使用旧属性创建新模型 解释 鉴于祖母绿模型: # app/models/emerald.rb class Emerald < ActiveRecord::Base validates_presence_of :name, :size, :shinyness, :color end 还有许多rspec测试,其功能如下: let(:shiny_emerald) { create(:eme

工厂女孩4.5.0

如何更改已传递属性的结构?例如,在处理大量现有测试时,在序列化现有字段时,希望工厂使用旧属性创建新模型

解释 鉴于祖母绿模型:

# app/models/emerald.rb
class Emerald < ActiveRecord::Base
  validates_presence_of :name, :size, :shinyness, :color
end
还有许多rspec测试,其功能如下:

let(:shiny_emerald) { create(:emerald, attributes: { name: 'Shiny', shinyness: 99 })}
let(:big_emerald)   { create(:emerald, attributes: { name: 'Big', size: 888 })}
当序列化祖母绿上的一些字段时,我们得到

# app/models/emerald.rb
class Emerald < ActiveRecord::Base

  serialize :emerald_properties, Hash

  validates_presence_of :name
  validates :emerald_properties, emerald_properties: true # This is a custom validator

  def size
    emerald_properties[:size]
  end
  def size=(value)
    emerald_properties[:size] = value
  end
  def shinyness
    emerald_properties[:shinyness]
  end
  def shinyness=(value)
    emerald_properties[:shinyness] = value
  end
  def colour
    emerald_properties[:colour]
  end
  def colour=(value)
    emerald_properties[:colour] = value
  end
end
到处都是,目的是保持

let(:big_emerald) { create(:emerald, attributes: { name: 'Big', size: 888 })}
并在工厂内进行调整

到目前为止,我已经:

FactoryGirl.define do
  factory :emerald do
    sequence(:name) {|n| "Emerald #{n} name"}
    emerald_properties ({ {size: 12, shinyness: 34, color: 'Dark Green'}.merge(attributes.slice(*[:size, :shinyness, :color])) })
  end
end

但它不起作用

只有在不进行内联操作时才需要“双括号”

误解

因此,在移除未使用的支架后,解决方案会起作用:

FactoryGirl.define do
  factory :emerald do
    sequence(:name) {|n| "Emerald #{n} name"}
    emerald_properties { {size: 12, shinyness: 34, color: 'Dark Green'}.merge(attributes.slice(*[:size, :shinyness, :color])) }
  end
end

同样,我会使用
切片,而不是不必要的
{}
,它们是不必要的
()

完全相同,除了
FactoryGirl.define do
  factory :emerald do
    sequence(:name) {|n| "Emerald #{n} name"}
    emerald_properties ({ {size: 12, shinyness: 34, color: 'Dark Green'}.merge(attributes.slice(*[:size, :shinyness, :color])) })
  end
end
# valid
favorites {{
  "PETC" => "http://www.petc.org"
}}
# invalid
favorites {
  "PETC" => "http://www.petc.org"
}
# invalid
favorites {{ "PETC" => "http://www.petc.org" }}
# valid
favorites { "PETC" => "http://www.petc.org" }
FactoryGirl.define do
  factory :emerald do
    sequence(:name) {|n| "Emerald #{n} name"}
    emerald_properties { {size: 12, shinyness: 34, color: 'Dark Green'}.merge(attributes.slice(*[:size, :shinyness, :color])) }
  end
end