开发者

AbstractController::DoubleRenderError with my respond_to in rspec

I got this Rails3 action:

def export
  respond_to do |format|
    format.tdl { render :xml => @template.export_as_tdl and return }
    format.json { render :json => @template.export_as_json }
  end
end

and filter before the export:

def find_environment
  @environment = KTEnvironment.find(params[:environment_id])
  raise HttpErrors::NotFound, _("Couldn't find environment '#{params[:environment_id]}'") if @environment.nil?
  @environment
end

and this rspec:

describe "export" do
  it "should call export_as_json" do
    @tpl.should_receive(:export_as_json)

    get :export, :id => TEMPLATE_ID
  end

  it "should call export_as_tdl" do
    @tpl.should_receive(:export_as_tdl)

    get :export, :id => TEMPLATE_ID, :format => 'tdl'
  end
end

I also defined the follo开发者_Go百科wing MIME type:

Mime::Type.register "application/tdl-xml", :tdl

When I try to run my rspec tests, I am constantly getting:

 1) Api::TemplatesController export should call export_as_tdl
 Failure/Error: get :export, :id => TEMPLATE_ID, :format => 'tdl'
 AbstractController::DoubleRenderError:
   Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
 # ./app/controllers/api/api_controller.rb:135:in `render_exception'
 # ./app/controllers/api/api_controller.rb:133:in `render_exception'
 # ./app/controllers/api/api_controller.rb:22:in `__bind_1314974553_619675'
 # ./spec/controllers/api/templates_controller_spec.rb:178

I have no clue what is happening there. This is my exception rendering code:

def render_wrapped_exception(status_code, ex)
  logger.error "*** ERROR: #{ex.message} (#{status_code}) ***"
  logger.error "REQUEST URL: #{request.fullpath}"
  logger.error pp_exception(ex.original.nil? ? ex : ex.original)
  orig_message = (ex.original.nil? && '') || ex.original.message
  respond_to do |format|
    format.json do
      render :json => {
        :errors => [ ex.message, orig_message ]
      }, :status => status_code
    end
    format.all do
      render :text => "#{ex.message} (#{orig_message})",
        :status => status_code
    end
  end
end

Ah so the render_exception general method in my api_controller is called. It looks like:

def render_exception(status_code, exception)
  logger.error pp_exception(exception)
  respond_to do |format|
    format.json { render :json => {:errors => [ exception.message ]}, :status => status_code }
    format.all  { render :text => exception.message, :status => status_code }
  end
end


Try disabling your error handler, the root cause should come up on its own.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜