Forum: Ruby on Rails what does "assert assigns(:users)" mean?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
dparkmit (Guest)
on 2007-07-26 18:25
(Received via mailing list)
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!
Phlip (Guest)
on 2007-07-26 18:32
(Received via mailing list)
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
dparkmit (Guest)
on 2007-07-26 18:49
(Received via mailing list)
thanks for the help!
Josh S. (Guest)
on 2007-07-27 05:03
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
Phlip (Guest)
on 2007-07-27 06:29
(Received via mailing list)
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
This topic is locked and can not be replied to.