Setting roles through rolify in FactoryBot definition

I would rather use FactoryBot’s after(:create) callback to create roles (also see this issue for Rolify).

Furthermore the method has_role? is used to check if a user has a specific role, to set a specific role you should use the add_role method.

FactoryBot.define do
  factory :user do
    name 'Test User'
    email '[email protected]'
    password 'please'
    password_confirmation 'please'
    # required if the Devise Confirmable module is used
    confirmed_at Time.now

    factory :admin do
        after(:create) {|user| user.add_role(:admin)}
    end

    factory :curator do
        after(:create) {|user| user.add_role(:curator)}
    end

    factory :super_admin do
        after(:create) {|user| user.add_role(:super_admin)}
    end
  end
end

Note: FactoryBot was previously called FactoryGirl

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)