I\'m fairly new to RoR and recently started learning BDD/Rspec for testing my application. I\'ve been looking for a way to spec an AJAX request, but so far I haven\'t found
Since Rails 5.0 (with RSpec 3.X), try setting xhr: true
like this:
get :index, xhr: true
Here's the relevant code in the ActionController::TestCase. Setting the xhr
flag ends up adding the following headers:
if xhr
@request.set_header "HTTP_X_REQUESTED_WITH", "XMLHttpRequest"
@request.fetch_header("HTTP_ACCEPT") do |k|
@request.set_header k, [Mime[:js], Mime[:html], Mime[:xml], "text/xml", "*/*"].join(", ")
end
end