Lazy evaluation and scoping

Take a look at the #should_require_login_for inside this pastie (it's a Shoulda macro): http://pastie.org/350658

#should_require_login_for takes an action name and a Proc. The method is meant to be used like this in my functional tests:

should_require_login_for   :new => Proc.new { get :new },   :create => Proc.new { post :create },   ...

What this is basically supposed to do is:

1) Loop through each action. 2) Create a Shoulda #context 3) #setup a test by calling the Proc containing "get :new" or something. 4) Assert a redirection to the login page inside the #should block.

So this should test if an action requires login or not. But my problem is, that I'm getting this error:

NoMethodError: undefined method `get' for PagesControllerTest:Class     ./test/functional/pages_controller_test.rb:7

Notice that the error is appearing in the PagesControllerTest and NOT inside the Shoulda macro. Using a lambda instead of a Proc simply "postpones" the error message to the Shoulda macro, but it's still the same message.

My question is: How do I get access to the #get, #post, #put, and #delete methods in my Proc or lambda? Any help appreciated. :slight_smile:

David Trasbo wrote:

My question is: How do I get access to the #get, #post, #put, and #delete methods in my Proc or lambda? Any help appreciated. :slight_smile:

If you need further details, please go ahead and ask. :slight_smile:

Take a look at the #should_require_login_for inside this pastie
(it's a Shoulda macro): http://pastie.org/350658

#should_require_login_for takes an action name and a Proc. The
method is meant to be used like this in my functional tests:

should_require_login_for :new => Proc.new { get :new }, :create => Proc.new { post :create }, ...

What this is basically supposed to do is:

1) Loop through each action. 2) Create a Shoulda #context 3) #setup a test by calling the Proc containing "get :new" or
something. 4) Assert a redirection to the login page inside the #should block.

So this should test if an action requires login or not. But my problem is, that I'm getting this error:

NoMethodError: undefined method `get' for PagesControllerTest:Class    ./test/functional/pages_controller_test.rb:7

Notice that the error is appearing in the PagesControllerTest and NOT inside the Shoulda macro. Using a lambda instead of a Proc simply "postpones" the error message to the Shoulda macro, but it's still the same message.

My question is: How do I get access to the #get, #post, #put, and #delete methods in my Proc or lambda? Any help appreciated. :slight_smile:

First off I know absolutely nothing about shoulda. That said blocks
are closures, so in particular they remember the value of self when
they were defined so if your test looks like

class SomeTestCase < Test::Unit::TestCase    some_macro :new => Proc.new {...} end

then for that block, self is and always will be SomeTestCase. The
easiest thing to do would be to have your setup method pass self to
the proc and have the proc call get/post/etc... on that.

Fred