ryanb/cancan — Issue #549: Testing a user can only list the users of her group
Author: afriqs
Created: 2012-01-19 17:14:17+00:00
State: open

Hi,

Well, I managed I think to implement such behavior (I tested the app) but unfortunately I did not manage to test it using RSpec :(

Imagine a User belongs to one Group and that the Group has many Users.

My UserController

```ruby
class UsersController < ApplicationController
load_and_authorize_resource

def index
end
end
```

My UserAbility

```ruby
class UserAbility
include CanCan::Ability

def initialize(user)
if user
can :index, User, :group_id => user.group.id
end
end
end
```

And finally the tests which fail

```ruby

it "should be able to list all the users of her group" do
@group = Fabricate(:group)
@user = Fabricate(:user, :group => @group)
@ability = UserAbility.new(@user)
# Following does not work :(
@ability.should be_able_to(:index, @group.users)
@ability.should be_able_to(:index, User.where(:group_id => @group.id))
end
```

If any of you has an any how to accurately test such a behavior ;-))

Note : Rails 3.1.3, Cancan 1.6.7, RSpec 2.8.0

Cheers