FactoryGirl.define do # 使用する際は:team1を指定する # シンボルがそのままクラス名ならclassは省略できる # name、addressがモデルの属性 factory :team1, class: Team do name :team1.to_s address '住所' end end
リレーションの関係にある複数のモデルを連動して追加したいときの方法いろいろ. リレーション関係なくても、インスタンス生成と同時に特定の処理を実行したいときにも使える. 呼び出し側でcreateにブロック渡す rspecなどのテストコード側でカスタムに関連データを追加する方法. shop has_many staffsのリレーションができている前提で、以下のようにbuildやcreateにブロックを渡せば、ブロック内で生成されたインスタンスを自由に修正できる. # # 生成されたインスタンスの内容をブロック内で自由に修正できる # shop = FactoryBot.build(:shop) do |s| s.name = "あいうえお" end # # 永続化せずにインスタンス生成 # shopにひもづくstaffをブロック内で追加している # shop = FactoryBot.buil
I have a User model that belongs to a Group. Group must have unique name attribute. User factory and group factory are defined as: Factory.define :user do |f| f.association :group, :factory => :group # ... end Factory.define :group do |f| f.name "default" end When the first user is created a new group is created too. When I try to create a second user it fails because it wants to create same group
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く