如何在 ActionController::TestCase 请求中设置内容类型
我试图在我的测试用例中执行 get 操作,如下所示:
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]
尽管在我的控制器中我有:
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
我到底应该如何在测试用例上编写预期的请求?
当我在浏览器中请求alarm_events.json时,它工作正常。
谢谢
I was trying to perform a get in my TestCase like this:
request.env['CONTENT_TYPE'] = 'application/json'
get :index,:application_name=>"Heka"
Though, it fails with a:
ActionView::MissingTemplate: Missing template alarm_events/index with {:handlers=>[:builder, :haml, :erb, :rjs, :rhtml, :rxml], :locale=>[:en, :en], :formats=>[:html]
Despite that in my controller I have:
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
How the hell should I code the intended request on the TestCase?
When I request alarm_events.json in the browser it works fine.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我必须在参数中指定格式以进行动作控制器测试:
I had to specify the format in the params for action controller testing:
我建议在 format.json 中设置标头
I would suggest to set the header inside the format.json