[rspec-users] Specing based on user roles

Nick Hoffman nick at deadorange.com
Tue Nov 11 17:30:36 EST 2008


On 2008-11-11, at 17:24, Fernando Perez wrote:
>> I've really moved away from shared example groups and started writing
>> more targeted macros. So I might do something like this:
>>
>> def for_roles *roles
>>  roles.each do |role|
>>    before(:each) { login_as role }
>>    yield
>>  end
>> end
>>
>> describe OrdersController do
>>  describe "GET index" do
>>    for_roles :admin, :sysadmin do |role|
>>      it "..." do ... end
>>    end
>>    for_roles :sysadmin do |role|
>>      it "..." do ... end
>>    end
>>  end
>>
>>  describe "GET edit" do
>>    for_roles :admin, :sysadmin do |role|
>>      it "..." do ... end
>>    end
>>    for_roles :sysadmin do |role|
>>      it "..." do ... end
>>    end
>>  end
>> end
>>
>
> I can't write my specs so that they work as expected. What does  
> login_as
> look like? And where do you put this code? I am not sure mine (if
> working) gets initialized correctly.
>
> I think I have to write my specs from scratch.

Hi Fernando. #login_as is a custom method that you need to write  
yourself. It should simply login as the given user, or a user with the  
given role. Its implementation will depend on which authentication and  
authorisation system you're using.

Cheers,
Nick



More information about the rspec-users mailing list