Matt Elhotiby
Matt Elhotiby

Reputation: 44066

How do I create an admin role with Factory girl?

Ok so i want to test out features that need an admin user and i am trying to login as admin but in order to do that i need to have an admin user

here is my code

  let(:user) { FactoryGirl.create(:user) }
  let(:admin_role) { FactoryGirl.create(:role) }

FactoryGirl.define do
  factory :user do
    first_name "John"
    last_name  "Doe"
    email  "[email protected]"
  end

  factory :role do
    name "Admin"
  end
end

how do i connect them I tried user.roles << user_role but got this error

/Users/matt/Sites/application/spec/controllers/directory_controller_spec.rb:16:in `block (3 levels) in <top (required)>': undefined local variable or method `user' for #<Class:0x007fa550890d80> (NameError)

Here is my models

class User < ActiveRecord::Base
    has_many :roles, :through => :role_users
    has_many :role_users
    ...

class Role < ActiveRecord::Base
    has_many :users, :through => :role_users
    has_many :role_users
    ...

class RoleUser < ActiveRecord::Base
    belongs_to :role
    belongs_to :user
end

Upvotes: 4

Views: 3099

Answers (3)

Marlin Pierce
Marlin Pierce

Reputation: 10089

spec/factories/user.rb

FactoryGirl.define do
  factory :user do
    first_name "John"
    last_name  "Doe"
    email "[email protected]"
  end

  factory :admin_user, :parent => :user do
    roles { [ FactoryGirl.create(:admin_role) ] }
  end

  factory :role do
    name { "Role_#{rand(9999)}" }
  end

  factory :admin_role, :parent => :role do
    name "Admin"
  end
end

Upvotes: 8

Neal Pisenti
Neal Pisenti

Reputation: 536

I was having the exact same problem, with code like:

describe "..." do
  let(:user) { create :user }
  let(:manager) { create :manager_role }
  user.roles << manager

  it "should ...." do
    # etc.
  end

end

# run rspec...
# => `block (2 levels) in <top (required)>': undefined local variable or method `user' for #<Class:0x000001016a2dc0> (NameError)

When I moved the user.roles << manager code into a before(:each) block, the errors disappeared. Thus, something like:

describe "..." do
  let(:user) { create :user }
  let(:manager) { create :manager_role }

  before(:each) do
    user.roles << manager
  end

  it "should ...." do
    # etc.
  end

end

This saves the pain of having to specify the role <-> user association in your factories.

I'm not familiar enough with how rspec and factory_girl work to understand why you can't have code like user.roles << role immediately after calling let(:user) { create :user }, but putting it into a before(:each) block or into an individual it "should ... " do block should do the trick.

Upvotes: 0

Matt Elhotiby
Matt Elhotiby

Reputation: 44066

Solved

Factory.sequence(:email) {|n| "person#{n}@example.com" }

Factory.define :role do |r|
  r.name {"Admin"}
end

Factory.define :role_user do |ru|
  ru.association(:role)
  ru.association(:user)
end

Factory.define :user do |u|
  u.first_name {"matt"}
  u.last_name {"jones"}
  u.email {Factory.next(:email)}
end

and using it like

  let(:role_user) { FactoryGirl.create(:role_user)  }
  before { sign_in(role_user) }

Upvotes: 0

Related Questions