[rspec-users] Specing based on user roles

Fernando Perez lists at ruby-forum.com
Thu Nov 6 16:20:29 EST 2008

> And why wouldn't you want to test that?
I want to test for it, it's just that I don't want to copy/paste spec 
like an idiot.

> 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

> This scheme makes it easy when we decide to remove a privilege from
> admin but keep it in sysadmin. Just move that example (it "...") to
> the other for_roles block.

I like your idea very much, very clean, readable and maintainable.

And thank you Mark, I will also use your idea to factor out 
commonalities in my specs.
Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list