[rspec-users] how to mock/stub restful_authenticated and acl2-ized act

Jarkko Laine jarkko at jlaine.net
Tue Apr 8 16:40:20 EDT 2008


On 8.4.2008, at 23.17, Giuseppe Bertini wrote:

> Hi Glenn, thanks for the response.
>
>> I would do something like this:
>>
>> user = mock_model(User, :operator => true)
>> controller.stub!(:current_user).and_return(user)
>
> OK, so, :operator is not a User attribute. Rather, a user is an
> operator, so to speak, if a habtm relationship exists between the user
> and the role titled 'operator'. If I modify your code like this:
>
>    role = mock_model(Role, :title => 'operator')
>    user = mock_model(User, :roles => [role])
>    controller.stub!(:current_user).and_return(user)
>
> things appear to be working well.
> Thank you again for your help!

You might want to take that one step further and create a method such  
as has_role?(:operator) for the user class. That way you don't have to  
stub arrays  like roles in your specs, you just stub that method to  
return true or false and you're done. That way you make your code more  
easily "speccable":

def has_role?(role)
   roles.map(&:title).include?(role.to_s)
end

//jarkko

>
>
> Giuseppe
> -- 
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2417 bytes
Desc: not available
Url : http://rubyforge.org/pipermail/rspec-users/attachments/20080408/a432b19c/attachment.bin 


More information about the rspec-users mailing list