# Help needed using assigns() functionality in functional test

**URL:** <https://www.ruby-forum.com/t/help-needed-using-assigns-functionality-in-functional-test/95860>\
**Category:** Engines\
**Created:** [January 20, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/help-needed-using-assigns-functionality-in-functional-test/95860 "2006-01-20T20:14:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [January 20, 2006, 8:14pm UTC](https://www.ruby-forum.com/t/help-needed-using-assigns-functionality-in-functional-test/95860/1 "2006-01-20T20:14:20Z")

</div>

I’m trying to create a functional test where the controller action (for  
now, anyway) breaks apart a string and populates intermediate variables  
with the results. I want to check the intermediate vars in my test, so I  
tried the following

(in the controller)

def string\_action

# do some processing, etc.

@string\_segment = “howdy”  
end

(in the unit test)  
def test\_the\_string\_doohicky  
post :string\_action, … some params here, etc.  
assert\_equal “howdy”, assigns[“string\_segment”]  
end

The assigns[] always comes up nil, so I must be missing something here.  
Any pointers?

TIA,  
Keith

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [January 20, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/help-needed-using-assigns-functionality-in-functional-test/95860/2 "2006-01-20T21:38:43Z")

</div>

DANG - I posted this in the wrong place :-(. Sorry

Keith

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [January 20, 2006, 10:10pm UTC](https://www.ruby-forum.com/t/help-needed-using-assigns-functionality-in-functional-test/95860/3 "2006-01-20T22:10:32Z")

</div>

I think you want this instead:

assert\_equal “howdy”, assigns(:string\_segment)

Lance

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [January 20, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/help-needed-using-assigns-functionality-in-functional-test/95860/4 "2006-01-20T22:19:39Z")

</div>

> (in the unit test)  
> def test\_the\_string\_doohicky  
> post :string\_action, … some params here, etc.  
> assert\_equal “howdy”, assigns[“string\_segment”]  
> end

What happens if you use  
assigns(:string\_segment)  
instead?

–

> **[snowblink](https://snowblink.co.uk/)**
>
> snowblink.co.uk : Homepage

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [January 20, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/help-needed-using-assigns-functionality-in-functional-test/95860/5 "2006-01-20T22:19:39Z")

</div>

On 1/20/06, Lance B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I think you want this instead:
> 
> assert\_equal “howdy”, assigns(:string\_segment)
> 
> Lance

Bad form to follow up my own post, but I’ll elaborate.

You don’t necessarily have to use a symbol, you could use the  
“string\_segment” string if you want, but all of the cool kids use  
symbols.  
🙂

I am not sure when the assigns hash changed to a method call, but the  
info  
in this doc seems to be outdated (  
[Peak Obsession](http://manuals.rubyonrails.com/read/chapter/28#page233)). If you look at  
the  
rails code, action\_controller/test\_process.rb has this method:

module Test  
module Unit  
class TestCase #:nodoc:

# …

```
   def assigns(key = nil)
      if key.nil?
        @response.template.assigns
      else
        @response.template.assigns[key.to_s]
      end
    end

```

# …

end

So it appears that the hash itself has moved into @response.template and  
that you’re calling this method when you are testing for assignments in  
your  
controller tests.

Lance
