# How I access controller instance variables from functional t

**URL:** <https://www.ruby-forum.com/t/how-i-access-controller-instance-variables-from-functional-t/48837>\
**Category:** Rails\
**Created:** [November 20, 2005, 2:36am UTC](https://www.ruby-forum.com/t/how-i-access-controller-instance-variables-from-functional-t/48837 "2005-11-20T02:36:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan.bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dan.bikle](https://www.ruby-forum.com/u/dan.bikle)\
**Post date:** [November 20, 2005, 2:36am UTC](https://www.ruby-forum.com/t/how-i-access-controller-instance-variables-from-functional-t/48837/1 "2005-11-20T02:36:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dan.bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dan.bikle](https://www.ruby-forum.com/u/dan.bikle)\
**Post date:** [November 20, 2005, 2:44am UTC](https://www.ruby-forum.com/t/how-i-access-controller-instance-variables-from-functional-t/48837/2 "2005-11-20T02:44:10Z")

</div>

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](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](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![dan.bikle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dan.bikle](https://www.ruby-forum.com/u/dan.bikle)\
**Post date:** [November 20, 2005, 2:56am UTC](https://www.ruby-forum.com/t/how-i-access-controller-instance-variables-from-functional-t/48837/3 "2005-11-20T02:56:14Z")

</div>

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](mailto: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](http://RealityForge.org)
