4

Have an app where i'm using Rolify and Pundit for authorization and Minitest/fixtures for testing.

When i setup a user as administrator using fixtures, i'm not getting back a true for an assertion.

/fixtures/users.yml
  administrator:
    email: admin@example.com
    roles: admin

/fixtures/roles.yml
  admin:
    id: 1
    name: admin

test.rb
  it "is administrator" do 
    User.first.has_role?('admin').must_equal true
  end

The test comes back as false, i've tried the admin with a symbol and still get the same thing.

When a user in that development environment has admin, i get true when i ask if the role is admin.

Test framework is minitest and my user model is rolified

Austio
  • 5,939
  • 20
  • 34
  • Did you check the query that gets fired when you use the method `has_role?`? – aledalgrande Aug 11 '14 at 22:19
  • SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = ? AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] – Austio Sep 11 '14 at 01:32

1 Answers1

3

Alright so the problem was related to setting up roles.yml correctly. I was using id: above and if you just use users: it works

/fixtures/users.yml
  administrator:
    email: admin@example.com
    roles: admin

/fixtures/roles.yml
  admin:
    users: administrator
    name: admin

test.rb
  it "is administrator" do 
    User.first.has_role?('admin').must_equal true
  end
Austio
  • 5,939
  • 20
  • 34