以下の内容はhttps://chulip.org/entry/20121018/1350640512より取得しました。


setterがないclassをFactoryGirlで使うとき

FactoryGirlで自分で作ったclassのFactoryを作りたかったんだけど
setterがないので普通のやり方ではデータをセットできなかった


class

#-*- coding: utf-8 -*-
class Hoge
  attr_reader :name

  def initialize(name)
    @name = name
  end
end

factory

#-*- coding: utf-8 -*-
FactoryGirl.define do
  factory :hoge, class: Hoge do
    name 'ほげ'
  end
end

普通のやり方ってのが上記の方法。


FactoryGirlのcallbackを使ってこうすれば。と教えてもらってやりたいことできた

#-*- coding: utf-8 -*-
FactoryGirl.define do
  factory :hoge, class: Hoge do
    after(:build) do |h|
      h.instance_variable_set(:@name, 'ほげ')
    end
  end
end



以上の内容はhttps://chulip.org/entry/20121018/1350640512より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14