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. :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. :)If you need further details, please go ahead and ask. :) -- David Trasbo. http://twitter.com/datra --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2 Jan 2009, at 17:12, David Trasbo wrote:> > 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. :)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> > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:>> :new => Proc.new { get :new }, >> >> >> My question is: How do I get access to the #get, #post, #put, and >> #delete methods in my Proc or lambda? Any help appreciated. :) > > First off I know absolutely nothing about shoulda.Shoulda is actually just a collection of test helpers: the #setup method that runs before each test, the #should that defines a test, and the #context that encapsulates a context, e.g.: context "GET new" do context "when logged in" do setup do log_in get :new end should_respond_with :success should "do something" do assert something end end end> 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.That might work. I tried a different approach, it''s not quite as elegant but I use it this way: should_require_login :post, :create should_require_login :get, :edit, :id => Page.first.id Take a look at the method definition here: http://pastie.org/351489 The first argument is the HTTP method you want to use. Second argument is what action to test. And the third is additional parameters you want to pass to the action. It works fine, but thanks anyway, Frederick. If anyone else could find the #should_require_login method helpful, you are free to use it. :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---