Question

class Ability

include CanCan::Ability

def initialize(user)

user ||= User.new # guest user

if user.has_role?  :student
  can :create, Atendimento
end

if user.has_role?  :professor
  can :create, Atendimento
end
if user.has_role? :administrative
  can [:read, :create], [Atendimento]
  can [:edit, :update], Atendimento
  can :manage, [Type, Place]
 end
if user.has_role? :admin
  can :manage, :all
end
end
end

and my factory

FactoryGirl.define do
factory :user do |f|
f.name "Alessandro"
f.username "alessandrocb"
f.matricula "123456789"
f.password "123456789"
f.password_confirmation "123456789"
f.after(:create) {|user| user.add_role(:student)}
end

I need those mocks receive all roles , but now I can only student role

my test with rspec

subject(:ability){ Ability.new(user) }
let(:user){ nil }

 context "when is an User" do
 let(:user) { FactoryGirl.create(:user) } 

what is happening is this: I can only test with rspec with only 1 paper, but would like to test with all the cancan, I need to create the factory with all these possibilities for different roles

Was it helpful?

Solution

First solution

FactoryGirl.define do
  factory :user do
    name "Alessandro"
    username "alessandrocb"
    (...)
    trait :student do
      after(:create) {|user| user.add_role(:student)}
    end
    trait :professor do
      after(:create) {|user| user.add_role(:professor)}
    end
    trait :administrative do
      after(:create) {|user| user.add_role(:administrative)}
    end
    trait :admin do
      after(:create) {|user| user.add_role(:admin)}
    end
  end
end

You can then use and combine these traits like this:

# Create a student
create :user, :student

# Create a user who is both professor and admin
create :user, :professor, :admin

Second solution

FactoryGirl.define do
  factory :user do
    name "Alessandro"
    username "alessandrocb"
    (...)
    ignore do
      role
    end
    after(:create) do |user, params|      
      user.add_role(params.role)  if params.role
    end
  end
end

And then:

# Create a student
create :user, role: :student

Note that the second solution does not allow you to combine roles as it is. But you could use an array to achieve this.

OTHER TIPS

I recently ran into a similar issue. Here's my users factory:

FactoryGirl.define do

  sequence :email do |n|
    "user#{n}@example.com"
  end

  factory :user do
    email
    password 'password'

    factory :admin_user do
      role 'administrator'
    end

    factory :support_user do
      role 'support'
    end

    factory :editor_user do
      role 'editor'
    end

    factory :sales_user do
      role 'sales'
    end

    factory :author_user do
      role 'author'
    end

    factory :guest_user do
      role 'guest'
    end

  end

end

From there I can just call the relevant factory for a spec:

create(:editor_user)

Or, depending on your User model and it's attendant properties, you could also build factories like:

create(:user, role: 'guest') # my User model has a properly called 'role'

I have 3 different users in my project: default, merchant, admin.

I have one file that handles the conditions. Note: this is FactoryBot and specifically factory bot rails. I am also using the gem Faker.

edit: the numbered roles are using enum, which converts the number in a string according to an array I defined. More on enums: https://naturaily.com/blog/ruby-on-rails-enum

factories/user.rb

// factories/user.rb
FactoryBot.define do
  factory :user do
    name  { Faker::Name.first_name }
    street_address { Faker::Address.street_address }
    city { Faker::Address.city }
    state { Faker::Address.state }
    zip { Faker::Address.zip }
    email { Faker::Internet.email }
    password { Faker::Internet.password }
    trait :default_user do
      role { 0 }
    end
    trait :admin_user do
      role { 1 }
    end
    trait :merchant_user do
      role { 2 }
    end
  end
end

spec file

// a spec file
RSpec.describe 'User logging in' do
  let(:user) { create(:user, :default_user) }
  let(:admin) { create(:user, :admin_user) }
[...]
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top