Set Rspec default GET request format to JSON

Drazen Mokic picture Drazen Mokic · Jun 13, 2012 · Viewed 33.3k times · Source

I am doing functional tests for my controllers with Rspec. I have set my default response format in my router to JSON, so every request without a suffix will return JSON.

Now in rspec, i get an error (406) when i try

get :index

I need to do

get :index, :format => :json

Now because i am primarily supporting JSON with my API, it is very redundant having to specify the JSON format for every request.

Can i somehow set it to default for all my GET requests? (or all requests)

Answer

Pratik Khadloya picture Pratik Khadloya · May 5, 2013
before :each do
  request.env["HTTP_ACCEPT"] = 'application/json'
end