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

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!

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!

thanks for the help!

Josh Susser 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. (-;