What does "assert assigns(:users)" mean?


#1

Hi All,

I recently started using RoR and love it! I’m currently creating some
unit tests and found this line in the scaffolding for a controller
test: assert assigns(:users)

What does it mean? I know what assert does but couldn’t find any
documentation on the function assigns.

Thanks for your help!


#2

dparkmit wrote:

I recently started using RoR and love it! I’m currently creating some
unit tests

Remember to alternate frequently between testing and coding, to see
what your new code is doing!

and found this line in the scaffolding for a controller
test: assert assigns(:users)

What does it mean? I know what assert does but couldn’t find any
documentation on the function assigns.

Me neither - I learned it after joining a team already doing Rails
projects.

It means a controller said @users = something. You can fetch out any
new controller instance variable with assigns().

I would also use assert_select() or assert_xpath() to parse the
rendered HTML and find evidence the controller’s data appeared there.
Just because a controller assigned something doesn’t mean the user can
see it!


Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
http://tinyurl.com/23tlu5 <-- assert_raise_message


#3

thanks for the help!


#4

Phlip wrote:

dparkmit wrote:

I recently started using RoR and love it! I’m currently creating some
unit tests

Remember to alternate frequently between testing and coding, to see
what your new code is doing!

and found this line in the scaffolding for a controller
test: assert assigns(:users)

What does it mean? I know what assert does but couldn’t find any
documentation on the function assigns.

Me neither - I learned it after joining a team already doing Rails
projects.

It means a controller said @users = something. You can fetch out any
new controller instance variable with assigns().

I would also use assert_select() or assert_xpath() to parse the
rendered HTML and find evidence the controller’s data appeared there.
Just because a controller assigned something doesn’t mean the user can
see it!

By the way, it’s better to say: assert_not_nil assigns(:users)
The #assert method test if a value is a true value (not false or nil).
It’s a bit vague for testing whether @users is not nil. Someone should
fix those scaffold tests, they confuse everyone.


Josh S.
http://blog.hasmanythrough.com


#5

Josh S. wrote:

By the way, it’s better to say: assert_not_nil assigns(:users)

Next, it’s better to lock down the exact list of users (check my
syntax):

assert_equal %w(Alice Bob Carl), assigns(:users).map(&:login)

The #assert method test if a value is a true value (not false or nil).
It’s a bit vague for testing whether @users is not nil. Someone should
fix those scaffold tests, they confuse everyone.

Just don’t do what I did. (-;


Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
http://tinyurl.com/23tlu5 <-- assert_raise_message