Forum: Ruby on Rails Lazy evaluation and scoping

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
941120dac2e97cf8bc1cbe0216159104?d=identicon&s=25 David Trasbo (datra)
on 2009-01-02 18:12
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. :)
941120dac2e97cf8bc1cbe0216159104?d=identicon&s=25 David Trasbo (datra)
on 2009-01-03 12:08
(Received via mailing list)
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-03 12:15
(Received via mailing list)
On 2 Jan 2009, at 17:12, David Trasbo 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. 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
941120dac2e97cf8bc1cbe0216159104?d=identicon&s=25 David Trasbo (datra)
on 2009-01-03 12:43
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. :)
This topic is locked and can not be replied to.