Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Testing controller's GET method - trying to use JSON and getting 406 errors

I have a simple controller that specifies:

respond_to :json

When I try to build a functional test that calls it like this:

test "GET" do
  get 'index', :format => :json
end

Everything works just fine. If, however, I try to pass a query parameter in like this:

test "GET" do
  get 'index', {:my_param = '1234'}, :format => :json
end

I get a 406 error returned from the controller. If I dump the response via response.inspect, I can see that the @status = 406 and the @header has a content-type of text/html. If I dump the response via response.inspect for the simple case that doesn't pass a query parameter, I see that the @header has a content-type of application/json as I request in the code.

Does anyone have any ideas what I'm doing wrong here? I'm suspecting that I'm screwing up ruby syntax with hashes or something but I'm bumping my head against the wall and not getting anywhere.

I'm on a Mac running Ruby 1.9 and Rails 3.0.5.

Thanks in advance!

like image 288
Bryan Avatar asked Apr 12 '11 14:04

Bryan


1 Answers

This is the function you are calling:

get(action, parameters = nil, session = nil, flash = nil) 

The :format part is still a parameter, certainly not a session or flash. Try:

get 'index', {:my_param => '1234', :format => :json}

Oh, important note, use '=>', not '=' ... that's a hash, not an assignment.

like image 102
DGM Avatar answered Oct 05 '22 07:10

DGM