[Rails] render_to_string in a controller fails under RSpec

View: New views
2 Messages — Rating Filter:   Alert me  

[Rails] render_to_string in a controller fails under RSpec

by Joel Young-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [Rails] render_to_string in a controller fails under RSpec

by David Chelimsky-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Nov 5, 2009 at 5:23 PM, Joel Young <skizzybiz@...> 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



For any case in which you want the controller to render views, you can say integrate_views.

HTH,
David

_______________________________________________
rspec-users mailing list
rspec-users@...
http://rubyforge.org/mailman/listinfo/rspec-users