Testing 123

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!

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.

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.

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.

On 10/11/06, Mike [email protected] wrote:

get :index
assert_response :success
assert_equal 1, assigns[:things].size

Perhaps…

assigns(:things).size

– James

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?

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. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com