Life is a YOLO game

如何在 factory_girl 中為有繼承關係的 Rails model 定義 Factory

August 07, 2016

問題

例如以下 Ruby on Rails 程式碼。

class Member < ActiveRecord::Base
  # has a string column named `email`
end

class Admin < Member
  # ...
end

MemberAdmin 兩個 Rails model 之間有繼承關係,那它們的 factory_girl Factory 要怎麼寫呢?

解答

FactoryGirl.define do
  factory :member do # Member model 的 factory
    email { Faker::Internet.email }
  end

  factory :admin, class: Owner, parent: :member
end

如果照著以上 code 定義 factory,則 Owner 的 factory 會繼承 Member factory 定義的所有欄位 (例如 email)。

參考資料:how to define factories with a inheritance user model


Henry Wu
Written by Henry Wu who lives and works in Taipei producing some nasty bugs.
Author: Henry Wu License: MIT License: CC BY-NC-ND 4.0