How I access controller instance variables from functional t


#1

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


#2

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


#3

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