Forum: Ruby on Rails How I access controller instance variables from functional t

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.
dan.bikle (Guest)
on 2005-11-20 03:36
(Received via mailing list)
People,

I noticed that instance variables in a controller
are automatically available to corresponding templates in the view.

Can any of you explain how this magic is implemented?

What I'd like to do is work some more of this magic so I can access
the instance variables from within functional tests.

Thanks,

-Dan
marcel (Guest)
on 2005-11-20 03:44
(Received via mailing list)
On Sat, Nov 19, 2005 at 05:35:36PM -0800, Dan B. wrote:
> I noticed that instance variables in a controller
> are automatically available to corresponding templates in the view.
>
> Can any of you explain how this magic is implemented?

It's in actionpack/lib/action_controller/base.rb

  add_variables_to_assigns

And then in actionpack/lib/action_view/base.rb

  assign_variables_from_controller

> What I'd like to do is work some more of this magic so I can access
> the instance variables from within functional tests.

In functional tests you can use the assigns method that is documented
here:
http://ap.rubyonrails.com/classes/Test/Unit/Assertions.html

In your controller:

  class FooController < ApplicationController
    def bar
      @baz = '...'
    end
  end

In your functional test:

  def test_bar
    assert_equal '...', assigns(:baz)
  end

marcel
--
Marcel Molina Jr. <removed_email_address@domain.invalid>
peter.j.donald (Guest)
on 2005-11-20 03:56
(Received via mailing list)
Hi,

The instance variables are already available to your functional
tests via the assigns method which returns a hash of all instance
variables (ie :symbol => value).

For example

assigns(:comment).description

accesses the description field of the comment instance variable
'@comment' from within my functional test after I have done the
get/post.

HTH,

On 11/20/05, Dan B. <removed_email_address@domain.invalid> wrote:
> People,
>
> I noticed that instance variables in a controller
> are automatically available to corresponding templates in the view.
>
> Can any of you explain how this magic is implemented?
>
> What I'd like to do is work some more of this magic so I can access
> the instance variables from within functional tests.

--
Cheers,

Peter D.
RealityForge.org
This topic is locked and can not be replied to.