Setting roles through rolify in FactoryBot definition
Asked Answered
K

1

32

I am using devise, rolify and cancan. I'm also using RSpec with FactoryBot for testing. Right now I'm working on some tests and I want to define users with different roles for those tests. Here is my current guess for how to do that using FactoryBot:

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
        self.has_role :admin
    end

    factory :curator do
        self.has_role :curator
    end

    factory :super_admin do
        self.has_role :super_admin
    end
  end
end

Here are some of my tests which don't seem to be written correctly: require 'spec_helper'

describe "Pages" do
    subject { page }

    before do
        @newpage = FactoryBot.create(:page)
        @newpage.save
    end

    context 'not logged in' do
        it_behaves_like 'not admin'
    end

    context 'logged in' do

        context 'as user' do
            before do
                @user = FactoryBot.create(:user)
                sign_in @user
            end

            it_behaves_like 'not admin'
        end

        context 'as curator' do
            before do
                @curator = FactoryBot.create(:curator)
                sign_in @curator
            end

            it_behaves_like 'not admin'
        end

        context 'as admin' do
            before do
                @admin = FactoryBot.create(:admin)
                sign_in @admin
            end

            it_behaves_like 'admin'
        end

        context 'as super admin' do
            before do
                @super_admin = FactoryBot.create(:super_admin)
                sign_in @super_admin
            end

            it_behaves_like 'admin'
        end
    end
end

When I run the specs I these are my errors:

1) Pages logged in as admin behaves like admin can show page 
 Failure/Error: Unable to find matching line from backtrace
 NoMethodError:
   undefined method `has_role=' for #<User:0x007f883384d178>
 Shared Example Group: "admin" called from ./spec/requests/pages_spec.rb:41
 # ./spec/requests/pages_spec.rb:37:in `block (4 levels) in <top (required)>'

2) Pages logged in as curator behaves like not admin can show page 
 Failure/Error: Unable to find matching line from backtrace
 ArgumentError:
   Factory not registered: curator
 Shared Example Group: "not admin" called from ./spec/requests/pages_spec.rb:32
 # ./spec/requests/pages_spec.rb:28:in `block (4 levels) in <top (required)>'

3) Pages logged in as super admin behaves like admin can show page 
 Failure/Error: Unable to find matching line from backtrace
 ArgumentError:
   Factory not registered: super_admin
 Shared Example Group: "admin" called from ./spec/requests/pages_spec.rb:50
 # ./spec/requests/pages_spec.rb:46:in `block (4 levels) in <top (required)>'

Note: FactoryBot was previously called FactoryGirl

Katzman answered 12/9, 2012 at 3:55 Comment(0)
S
64

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

Somersomers answered 18/9, 2012 at 13:43 Comment(2)
Thanks, I'll try this the next time. I have since moved on to another method for handling user roles.Katzman
Also look into before(:create), works as well but different callback.Benefield

© 2022 - 2024 — McMap. All rights reserved.