0
的respond_to我得到這個Rails3中動作:一個AbstractController :: DoubleRenderError我在rspec的
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
和過濾器出口前:
def find_environment
@environment = KTEnvironment.find(params[:environment_id])
raise HttpErrors::NotFound, _("Couldn't find environment '#{params[:environment_id]}'") if @environment.nil?
@environment
end
這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
我還定義了以下MIME類型:
Mime::Type.register "application/tdl-xml", :tdl
當我嘗試運行我的RSpec的測試中,我經常收到:
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
我不知道發生了什麼事那裏。這是我的例外渲染代碼:
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
啊所以我的api_controller中的render_exception一般方法被調用。它看起來像:
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
你能告訴哪一行'templates_controller_spec.rb:178'和'api_controller.rb:22'是? –
我瘋狂的猜測是,你的行爲不知何故引發了一個異常,這會混淆整個事情。你也可以在你的api控制器中發佈'render_exception'嗎? –
已添加。在行動之前還有一個過濾器。還加了一個。 – lzap