Setting roles through rolify in FactoryGirl definition

浪尽此生 提交于 2019-11-30 01:28:17
hjblok

I would rather use FactoryGirls 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.

FactoryGirl.define do
  factory :user do
    name 'Test User'
    email 'example@example.com'
    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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!