[rspec-users] A bit confused with the use of assign() to test views
dchelimsky at gmail.com
Wed Aug 3 07:37:14 EDT 2011
>> Keep in mind that rspec-rails is a thin wrapper around the built-in Rails testing framework. The convention of relating a symbol in the spec to an instance variable in the view was established by Rails with the `assigns` method in functional tests (controller specs in rspec):
>> thing = Factory(:thing)
>> get :index
>> assigns(:thing).should eq(thing)
>> In the last line, `assigns(:thing)` refers to the `@thing` instance variable in the view.
> Hello David
> Thanks for that. Doesn't assign have 2 arguments with the first being the variable to be assigned to and the second being the contents?
Yes, `assign`, in view specs, has two arguments. `assigns` (plural), in controller specs (and provided by Rails) takes only one. `assign` is a setter for a single instance variable, `assigns` is a getter which keys into a map of all assigned instance variables.
PS - I had to move your post to the bottom and quote my own comments from the previous email to which you were responding. Please read http://idallen.com/topposting.html and make it easier for people to be helpful to you in a way that is understandable to everyone else.
More information about the rspec-users