Michael MacDonald

Use response.capture instead

In Rails, Testing on July 15, 2009 at 8:00 am

Recently, I upgraded an app from Rails 2.1.0 to Rails 2.3.2. First, I upgraded rspec and rspec-rails to 1.2.7, raked my specs and they all passed. But after I upgraded to Rails 2.3.2, one particular spec failed with the following error message:

"undefined method downcase' for :extra_info:Symbol"

The spec was verifying that a piece of content existed in a content_for block in a view:

it "should render extra info" do
  render response[:extra_info].should have_tag("p", "Here is some extra info")
end

It seemed that something in Rails had changed and that either RSpec needed to be updated or I needed to rewrite the implementation of my spec.

Now, for whatever reason, I completely missed the deprecation warning in RSpec about this one. It would’ve been easy to resolve had I seen it. Unfortunately, the rspec.info website had no mention of it and still listed the old, and now out-of-date, information on it. After not turning up any answers via Google, searching the RSpec changelog and Lighthouse tickets, I ended up filing a ticket. Thankfully, David Chelimsky was kind enough to help me out. His answer was:

“Rails 2.3 added its own [] to TestResponse, so I deprecated this a while back with a note stating it would be removed once rails-2.3 went final: http://github.com/dchelimsky/rspec-rails/blob/50eeb344823057e860a1b1dea0d6ee53a391c7a3/lib/spec/rails/extensions/action_controller/test_response.rb It looks like I failed to add this to release notes (or at least I can’t find it), so I’ll figure out when that release was and update the release notes.”

So,  the correct code is:

it "should render extra info" do
  render response.capture(:extra_info).should have_tag("p", "Here is some extra info")
end
Advertisements
  1. Certainly much better than:

    
    response.template.instance_variable_get('@content_for_extra_info')
    

    *grin*

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: