Trying to spec login requirement with a shared example

In all of my controller specs I have tests that look something like

it “should require login” do
get :edit, :id => ‘7’
response.should redirect_to(new_session_path)

I’d like to move this to a shared example, but the request call (get()
or post()) varies with each example. Is there a good way to tell the
shared example how to request the page?

I thought about doing something like this

describe “whatever” do
def request_page
get :edit, :id => ‘7’

use shared example


then in the shared example I would call request_page() instead. Although
this is slightly more DRY it isn’t saving me any lines of code and it
feels like there could be a better way. Another idea I had was to make a
helper function that returns a Proc (basically I’m faking currying) and
I could bind that Proc to something like @request_proc and then run the
proc in the shared example.

Anyone have any better ideas?



Looking for last minute shopping deals?
Find them fast with Yahoo! Search.

I don’t claim that my idea/way is a good way to go about it since it is
kinda hackisk… but this is what I have been doing… In my
spec_helper.rb I’ll have the following:

module Spec::Example::ExampleGroupMethods

def it_should_require_a_user_to_be_logged_in
it “should redirect to the signup page when no user is logged in” do
send(“do_#{[:get, :post, :put, :delete].find{|verb| respond_to?
response.should redirect_to(signup_url(:service =>


The implementation of it_should_require_a_user_to_be_logged_in are of
course specific to my app but you get the picture with what I’m doing
with the send command… Again, I know this is hackish but it was the
best I could come up with. So it your example groups you can just say
stuff like:

describe UsersController, “handling PUT /users/1” do

before do
@user = stub_login(mock_user(:update_attributes => true))

def do_put
put :update, :id =>



Prior to this I would alias all of my do_put, do_get, etc… as
do_action, and then just call do_action. Hmm… that actually just gave
me an idea. You could use the method_added hook that ruby provides to
alias the do_get, do_put, etc… to do_action every time it is defined.
How does that sound?
I haven’t tested it but that might be the best way to do things now that
I think about it.

Hope that helps… If you try out the method_added idea let me know
since I would be interested.