Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set the Content Type in a ActionController::TestCase request

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

like image 920
Pedro Rolo Avatar asked Mar 28 '11 16:03

Pedro Rolo


2 Answers

I had to specify the format in the params for action controller testing:

get :index, {format: :json}
like image 190
tommy chheng Avatar answered Sep 27 '22 19:09

tommy chheng


@request.accept = 'application/json'
like image 23
Pedro Rolo Avatar answered Sep 27 '22 17:09

Pedro Rolo