Joel Young
2009-Nov-05 22:23 UTC
[rspec-users] [Rails] render_to_string in a controller fails under RSpec
I''m using render_to_string in a controller to send some JSON data with
an HTML fragment. Unfortunately, it seems that rspec-rails breaks that
by intercepting render and simply recording that a render was called.
Using rspec-rails and rspec versions 1.2.9, I''ve tracked it down to
lib/spec/rails/example/controller_example_group.rb:181 -- since it''s
calling record_render, it returns a Fixnum, so instead of an HTML
fragment, I get "1". I''ve confirmed that if I add a call to
"super" on
the next line, it does what I want. Obviously that would be bad for
other reasons, though.
Is there a workaround for this? Is there a better way to get an HTML
fragment included in the JSON? Using an RJS template won''t work, since
it wraps my JSON object in an exception handling block.
For reference, here''s the controller action that calls
render_to_string:
def changed
unless params[:since]
render :status => :bad_request
return
end
respond_to do |format|
format.js do
@changed = Event.changed_since(params[:since]).collect do |
event|
{
:id => dom_id(event),
:sidebar_html => render_to_string(:partial =>
"history_item", :object => event)
}
end
render :json => @changed
end
end
end
And here is the relevant part of the controller spec:
it "should return the sidebar HTML for the event" do
get :changed, :since => @time
json = JSON.parse(response.body)
json.should have(1).event
response.body = json.first["sidebar_html"]
response.should include_text("TestApp r1234")
end
David Chelimsky
2009-Nov-06 02:15 UTC
[rspec-users] [Rails] render_to_string in a controller fails under RSpec
On Thu, Nov 5, 2009 at 5:23 PM, Joel Young <skizzybiz at gmail.com> wrote:> I''m using render_to_string in a controller to send some JSON data with > an HTML fragment. Unfortunately, it seems that rspec-rails breaks that > by intercepting render and simply recording that a render was called. > Using rspec-rails and rspec versions 1.2.9, I''ve tracked it down to > lib/spec/rails/example/controller_example_group.rb:181 -- since it''s > calling record_render, it returns a Fixnum, so instead of an HTML > fragment, I get "1". I''ve confirmed that if I add a call to "super" on > the next line, it does what I want. Obviously that would be bad for > other reasons, though. > > Is there a workaround for this? Is there a better way to get an HTML > fragment included in the JSON? Using an RJS template won''t work, since > it wraps my JSON object in an exception handling block. > > For reference, here''s the controller action that calls > render_to_string: > > def changed > unless params[:since] > render :status => :bad_request > return > end > respond_to do |format| > format.js do > @changed = Event.changed_since(params[:since]).collect do | > event| > { > :id => dom_id(event), > :sidebar_html => render_to_string(:partial => > "history_item", :object => event) > } > end > render :json => @changed > end > end > end > > And here is the relevant part of the controller spec: > > it "should return the sidebar HTML for the event" do > get :changed, :since => @time > json = JSON.parse(response.body) > json.should have(1).event > response.body = json.first["sidebar_html"] > response.should include_text("TestApp r1234") > end >http://rspec.info/rails/writing/controllers.html For any case in which you want the controller to render views, you can say integrate_views. HTH, David -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20091105/16b0df15/attachment.html>