If your controller action looks like this:
respond_to do |format|
format.html { raise \'Unsupported\' }
format.js # index.js.erb
end
and yo
These three seem to be equivalent:
get :index, :format => 'js'
@request.env['HTTP_ACCEPT'] = 'text/javascript'
@request.accept = "text/javascript"
They cause the controller to use a js template (e.g. index.js.erb)
Whereas simulating an XHR request (e.g. to get a HTML snippet) you can use this:
@request.env['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest"
This means request.xhr? will return true.
Note that, when simulating XHR, I had to specify the expected format or I got an error:
get :index, format: "html"
Tested on Rails 3.0.3.
I got the latter from the Rails source, here: https://github.com/rails/rails/blob/6c8982fa137421eebdc55560d5ebd52703b65c65/actionpack/lib/action_dispatch/http/request.rb#L160