Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to define factories with a inheritance user model

I got following problem: In my application i use inheritance to define my user model:

class User  include Mongoid::Document   field :name...  field :bla... end   class CustomUser < User  field :customuserfield... end 

How can i write factories to map this Class hirachie in my specs. And keep up writing with don´t repeat yourself.

FactoryGirl.define do    factory :user do     name  "name"     bla "bla"      factory :custom_user do       customfield "customfield"     end   end end 

This doesn´t work for me because the class is also "User". With "User" i got a invalid error because the customfields are not defiend here. Is there a good practice, way or method to relize something like that.

like image 827
bulleric Avatar asked Nov 12 '12 12:11

bulleric


1 Answers

You can try this:

factory :user do   name  "name"   bla "bla" end  factory :custom_user, class: CustomUser, parent: :user do   customfield "customfield" end 

For more info: Inheritance.

like image 147
Thanh Avatar answered Sep 18 '22 09:09

Thanh