I'm using rails 4.2 and had a tough time with rolify and rails fixtures. After some mucking I finally got it working. Posting here incase someone else needs to do something similar.
- Create a
UsersRole
model inmodels/users_role.rb
# pretty much exclusively created for our fixtures to work
class UsersRole < ActiveRecord::Base
belongs_to :user
belongs_to :role
end
- Create fixtures like so:
# users.yml
some_user:
# roles.yml - key here is to NOT include "resource"
admin_role:
name: :admin
# users_roles.yml
users_roles1:
user: some_user
role: admin
- Add the fixtures to your tests
# in some test file
fixtures :users, :roles, :users_roles
users(:some_user).has_role? :admin # => true