# Testing 123

**URL:** <https://www.ruby-forum.com/t/testing-123/71821>\
**Category:** Rails\
**Created:** [October 11, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/testing-123/71821 "2006-10-11T18:37:35Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 11, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/testing-123/71821/1 "2006-10-11T18:37:35Z")

</div>

I’m having a super strange functional testing issue, and was hoping  
someone could provide some insight.

In my controller I have this in the index action:

@things = Thing.find(:all)

My index test looks like this:

get :index  
assert\_response :success  
assert\_equal 1, assigns[:things].size

But the second assert causes an error. assigns[:things] is always nil  
even though @things is getting assigned in the controller. If I do this  
in the same test:

assert\_equal 1, Thing.find(:all).size

It works. I have a reference to the things fixture in the test, so the  
one record should be in the test db, and the manual find proves that.

I’m not sure why my instance var is always nil though

Any thoughts?

Thanks!

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 11, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/testing-123/71821/2 "2006-10-11T18:49:59Z")

</div>

Mike wrote:

> I’m having a super strange functional testing issue, and was hoping  
> someone could provide some insight.
> 
> In my controller I have this in the index action:
> 
> @things = Thing.find(:all)
> 
> My index test looks like this:
> 
> get :index  
> assert\_response :success  
> assert\_equal 1, assigns[:things].size
> 
> But the second assert causes an error. assigns[:things] is always nil  
> even though @things is getting assigned in the controller. If I do this  
> in the same test:

Try assigns(:things), with () instead of [].

Due to some oddness with backwards compatibility or something like that  
the assigns object does not support the bracketed hash style notation.  
Although the brackets still work for params and session.

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 11, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/testing-123/71821/3 "2006-10-11T19:24:50Z")

</div>

Hey Alex.

Thanks for the reply. I actually tried with the parens instead, it does  
the same thing. Also, I use the brackets for assigns elsewhere without  
an issue.

Hmm…

Alex W. wrote:

> Mike wrote:
> 
> > I’m having a super strange functional testing issue, and was hoping  
> > someone could provide some insight.
> > 
> > In my controller I have this in the index action:
> > 
> > @things = Thing.find(:all)
> > 
> > My index test looks like this:
> > 
> > get :index  
> > assert\_response :success  
> > assert\_equal 1, assigns[:things].size
> > 
> > But the second assert causes an error. assigns[:things] is always nil  
> > even though @things is getting assigned in the controller. If I do this  
> > in the same test:
> 
> Try assigns(:things), with () instead of [].
> 
> Due to some oddness with backwards compatibility or something like that  
> the assigns object does not support the bracketed hash style notation.  
> Although the brackets still work for params and session.

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 11, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/testing-123/71821/4 "2006-10-11T20:19:20Z")

</div>

Alex W. wrote:

> Mike wrote:
> 
> > Hey Alex.
> > 
> > Thanks for the reply. I actually tried with the parens instead, it does  
> > the same thing. Also, I use the brackets for assigns elsewhere without  
> > an issue.
> > 
> > Hmm…
> 
> Apparently my info is out of date!
> 
> This won’t fix it, but it should give you some more info.
> 
> get :index  
> raise assigns.inspect
> 
> This will create an error and then dump the content of “assigns”. Does  
> it actually have the data in it?

And just to clarify, it seems [] and () are treated differently after  
all. In my tests, running on edge:

assert\_not\_nil assigns(‘products’) #works  
assert\_not\_nil assigns(:products) #works  
assert\_not\_nil assigns[‘products’] #works

assert\_not\_nil assigns[:products] #fails

so as long you dont do assigns[:things] you should be fine.

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 11, 2006, 8:28pm UTC](https://www.ruby-forum.com/t/testing-123/71821/5 "2006-10-11T20:28:36Z")

</div>

On 10/11/06, Mike [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> get :index  
> assert\_response :success  
> assert\_equal 1, assigns[:things].size

Perhaps…

assigns(:things).size

– James

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 11, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/testing-123/71821/6 "2006-10-11T20:07:20Z")

</div>

Mike wrote:

> Hey Alex.
> 
> Thanks for the reply. I actually tried with the parens instead, it does  
> the same thing. Also, I use the brackets for assigns elsewhere without  
> an issue.
> 
> Hmm…

Apparently my info is out of date!

This won’t fix it, but it should give you some more info.

get :index  
raise assigns.inspect

This will create an error and then dump the content of “assigns”. Does  
it actually have the data in it?

---

<div class="post-metadata">

**Author:** ![mike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mike](https://www.ruby-forum.com/u/mike)\
**Post date:** [October 15, 2006, 4:43am UTC](https://www.ruby-forum.com/t/testing-123/71821/7 "2006-10-15T04:43:30Z")

</div>

On Oct 11, 2006, at 11:19 AM, Alex W. wrote:

> > 
> 
> assert\_not\_nil assigns(:products) #works  
> assert\_not\_nil assigns[‘products’] #works
> 
> assert\_not\_nil assigns[:products] #fails
> 
> so as long you dont do assigns[:things] you should be fine.

Test::Rails in ZenTest fixes this. You also get an extra method you  
can drop in #teardown to verify you’ve asserted all of your assigns.

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)
