ruby-on-rails - 如何在 ActionController::TestCase 请求中设置内容类型

标签 ruby-on-rails ruby json ruby-on-rails-3 rest

我试图像这样在我的测试用例中执行获取:

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/

相关文章:

ruby-on-rails - 如何在heroku上从github上拉取插件?

javascript - 通过 Javascript 传递参数 [ :commit] value when calling form. submit()

ruby - jruby -S bundle install jruby : No such file or directory -- bundle (LoadError)

ruby - 运行 dependency.rb :247:in `to_specs' : Could not find bundler

ruby - Chef 错误 Chef::Util::FileEdit

java - 如何应用过滤器从 data.gov.in api 获取指定数据?

json - 从 GoogleApiBooks 解析 JSON

ruby-on-rails - Rack session key 对于 memcached 来说太长了

javascript - 用于 RoR 的 Lightbox/Facebox Gem

javascript - 使用 Javascript 使用 JSON 创建表