If your controller action looks like this:
respond_to do |format|
format.html { raise \'Unsupported\' }
format.js # index.js.erb
end
and yo
Pass in a :format
with your normal params to trigger a response in that format.
get :index, :format => 'js'
No need to mess with your request headers.
RSpec 3.7 and Rails 5.x solution:
A few of these answers were a little outdated in my case so I decided to provide an answer for those running Rails 5 and RSpec 3.7:
it "should render js" do
get :index, xhr: true
expect(response.content_type).to eq('text/javascript')
end
Very similar to Steve's answer with a few adjustments. The first being xhr
is passed as a boolean key/pair. Second is I now use expect
due to should
receiving deprecation warnings if used. Comparing the content_type
of the response to be equal to text/javascript
worked for me.
Set the accepted content type to the type you want:
@request.accept = "text/javascript"
Combine this with your get :index
test and it will make the appropriate call to the controller.