malcoauri
malcoauri

Reputation: 12189

Nested factory girl with nested attributes in Rails

There are 2 factories (using FactoryGirl):

FactoryGirl.define do

  factory :owner do
    name                                'Some name'
    email                               '[email protected]'
    phone                               '89020000000'
    password                            '123456'
    password_confirmation '123456'
    role
  end

end

And roles:

FactoryGirl.define do

  factory :role do
    description 'Owner'
  end

  factory :superuser_role do
    description 'Superuser'
  end
end

I want to add a :superuser factory that has the same fields as :owner except role. How can I do it? Thanks.

Upvotes: 0

Views: 190

Answers (1)

gotva
gotva

Reputation: 5998

I think this will work

FactoryGirl.define do

  factory :owner do
    name                                'Some name'
    email                               '[email protected]'
    phone                               '89020000000'
    password                            '123456'
    password_confirmation '123456'
    role

    factory :superuser do
      association :role, factory: :superuser_role
    end
  end

end

Upvotes: 2

Related Questions