我试图像这样在我的测试用例中执行获取:
request.env['CONTENT_TYPE'] = 'application/json'
get :index,:application_name=>"Heka"
虽然,它失败了:
ActionView::MissingTemplate: Missing template alarm_events/index with {:handlers=>[:builder, :haml, :erb, :rjs, :rhtml, :rxml], :locale=>[:en, :en], :formats=>[:html]
尽管在我的 Controller 中我有:
respond_to :html, :json
def index
@alarm_events=[...]
respond_with @alarm_events do |format|
format.json{
render :json=>@alarm_events.map{|e| e.to_portal_representation}.to_json,
:content_type=>'application/json'
}
end
end
我到底应该如何对 TestCase 上的预期请求进行编码?
当我在浏览器中请求 alarm_events.json 时,它工作正常。
谢谢
最佳答案
我必须在 Action Controller 测试的参数中指定格式:
get :index, {format: :json}
关于ruby-on-rails - 如何在 ActionController::TestCase 请求中设置内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5462330/