ruby on rails - Is it the correct way to implement belongs_to relation with factory girl? -
i have realy easy model. user have role_id depends of role table (id, name) role reference.
i want create users of types on rspec test, factory girl.
my first idea
factory :role name "guest" factory :role_admin name "admin" end factory :role_supervisor name "supervisor" end etc... have lot different roles end factory :user email password '123456' password_confirmation '123456' association :role, factory: :role factory :admin association :role, factory: :role_admin end factory :supervisor association :role, factory: :role_supervisor end etc... have lot different roles end
in model have simple method :
def is(role_name) return self.role.name == role_name end
it's correct way to? realy need create factory role? can make stub function in factory girl each role?
i realy new test stuff, thanks.
factories should reflect models.
class user has_many :products end class product belongs_to :user end factory :user products end factory :product end
if want have special cases (understand roles in case) can define traits
:
factory :user traits :admin end factory :admin_user, traits: [:admin] end
more information on traits here.
Comments
Post a Comment