Assigns in RSpec 2 - deprecation warning on rendered?

I have come to the views_example in the RSpec book, using RSpec 2 all
the way.
Now I have this spec:

describe “messages/show.html.erb” do
it “displays the text attribute of the message” do
assigns[:message] = stub(“Message”, :text => “Hello world!”)
puts “assigns @message: #{@message}”
@message = stub(“Message”, :text => “Hello world!”)
puts “@message: #{@message}”
render
rendered.should contain(“Hello world!”)
end
end


$ rspec spec/views/messages/show.html.erb_spec.rb

assigns @message:
@message: #[RSpec::Mocks::Mock:0x8244a124 @name=“Message”]


DEPRECATION WARNING: you are using deprecated behaviour that will
be removed from a future version of RSpec.

/Users/kristianconsult/Development/Languages/Ruby/Apps/Web-apps/Rails/
Rails-3/Experimental/views_example/spec/views/messages/
show.html.erb_spec.rb:10:in `block (2 levels) in <top (required)>’

  • response is deprecated.
  • please use rendered instead.

  1. I notice that the assigns doesn’t seem to work with RSpec 2. An
    easy fix is to set the instance var directly. Is this the new way?

  2. I changed ‘response’ to ‘rendered’ as the variable I get back from
    calling ‘render’, but I still get a deprecation warning! Has it been
    changed to a new name again without the deprecation check having been
    updated?

Hi Kristian,

The new rspec 2 way of doing assigns is assign(variable_name,
whats_being_assigned). So your example becomes:

assign(:message, stub(“Message”))

That should get rid of your deprecation message.

Cheers,
Trey

assign(:message, stub(“Message”))

That should get rid of your deprecation message.

Yeah! But strange deprecation message that it mentions:

  • response is deprecated.
  • please use rendered instead.

When it should be:

  • assigns is deprecated.
  • please use assign instead.

Also, how do I use this assigned message

In my show view I have:
<%=h @message.text %>

When I run it now:

assign(:message, stub("Message"))
puts "@message: #{@message.inspect}"
render

@message: nil <--------- OUCH!!!
F

  1. messages/show.html.erb displays the text attribute of the message
    Failure/Error: Unable to find matching line from backtrace
    Stub “Message” received unexpected message :text with (no args)

@message is nil after using assigns! But inside the view it seems at
least to recognize the stub called “Message”?

Now if I inspect @message in the view instead
<%=h @message.inspect %>

expected the following element’s content to include “Hello world!”:
#<RSpec::Mocks::Mock:0x81952668 @name=“Message”>

Now I just need to find out how to extract the text from the Stub
message… ?

If you want to use your assigned message within your spec, you can
assign it
to a variable in the assign call, e.g.

assign(:message, @message = stub(‘message’).

Since you’re calling text on the message in your view, you should stub
that
method:

assign(:message, @message = stub(‘message’, :text => ‘hello world’).

As to the deprecation message, if you’re still using response, you
should be
using rendered. The message is correct.

Hope the helps ya,
Trey