Bohn Bohn - 1 year ago 54
Ruby Question

RSpec test to catch 406 errors if the format of accept header is different

I am trying to write a RSpec test to test for wrong content type passed into the request, because in the controller side I am only supporting JSON.

So currently I have my RSpec written like this:

it 'returns a 406 if the request is not for json' do
request.accept = 'text/html'
get :show
expect(response.code).to eq('406')

and the show method in my controller is like this:

respond_to :json

def show

org_id = Organization.find_by_id(params[:id])

@organization = Organization.includes([:BunchOfTablesBlah).find(params[:id])


But that test fails with this error:

1) PopulationManagementController show an organization returns a 406
if the request is not for json
Failure/Error: get :show
No route matches {:controller=>"population_management", :action=>"show"}
# ./spec/controllers/population_management_controller_spec.rb:184:in
`block (3 levels) in '

Answer Source
No route matches {:controller=>"population_management", :action=>"show"}

ActionController#show requires an ID, but you haven't supplied one, thus the routing failure.

Try get :show, :id => '1'