james2m / canard

Makes role based authorization in Rails really simple. Wraps CanCan and RoleModel up with a smattering of syntactic sugar, some generators and scopes.
MIT License
125 stars 28 forks source link

How to stub the ability in the step_definitions #27

Closed nagaharish closed 9 years ago

nagaharish commented 9 years ago

Hi, I am having a code like can? :access, :project in my views. Based on that I am showing the link. So that is defined in the abilities file as usual which is working fine.

i.e., eg: app/abilities/admin.rb I am using can :access, :project

app/abilities/employee.rb I am using cannot :access, :project

Now I wanted to test in the following way in cucumber with the help of step_definition:

So this means I wanted to dynamically change the ability and ensure the test pass. How DO I do this? I have done something like this which is not working.

@user.ability.stub(:can?).with(:access, :project).and_return(false)

The reason behind this is tomorrow we might change employee ability also to can :access, :project hence i want this dynamic behavior.

james2m commented 9 years ago

Your controller will have current_ability as implemented by cancan(can) so that's where you'd stub. But I'd first say you shouldn't be stubbing an integration test :)

nagaharish commented 9 years ago

Hi Thank you. I actually don't wanted to change it dynamically always. The behavior might change every month depending on the market requirements. So that point of time we will change it in the app/abilities then the problem will be solved. The idea is that point of time we don't wanted to start fixing the test cases again.

Hence we wanted to stub it so all the test cases will be fine.

I hope this clarifies my question.

james2m commented 9 years ago

Then you'll need to mock and stub the current_ability in the controller.