[rspec-users] A bit confused with the use of assign() to test views
dchelimsky at gmail.com
Wed Aug 3 06:46:33 EDT 2011
On Aug 3, 2011, at 3:50 AM, ct9a wrote:
> also, i have just read a little more in the rspec book.
> here's an extract:
> ------------- extract start ---------------------
> View specs expose an assign method, which we use to provide data to
> the view. Modify the spec as follows:
> describe "messages/show.html.erb" do
> it "displays the text attribute of the message" do
> assign(:message, double("Message", :text => "Hello world!"))
> rendered.should contain("Hello world!")
> The new first line of the example creates a test double, which stubs
> the text( ) method with a return value of “Hello world!” and assigns
> it to an @message instance variable on the view.
> ------------- extract end ---------------------
> If :message (in the view spec) can correspond to @message variable (in
> the actual show.html.erb view), is this a Rspec convention/thing?
> Sorry, Im just trying to find more resources to read up on rspec but
> i'm having not much luck. Appreciate your thoughts.
> Thank you .
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)
In the last line, `assigns(:thing)` refers to the `@thing` instance variable in the view.
More information about the rspec-users