問題

例如以下 Ruby on Rails 程式碼。

1
2
3
4
5
6
7
class Member < ActiveRecord::Base
# has a string column named `email`
end

class Admin < Member
# ...
end

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

解答

1
2
3
4
5
6
7
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