Request specs have a lot of benefits over traditional controller specs. They allow for full stack testing at the controller level. One thing I recently ran into was how to write this for AJAX requests.
If you’re familiar with controller specs then you might have seen the following format before:
xhr :get, :new
For an endpoint that uses AJAX, I first tried the following request spec style:
get users, params: { name: 'Geralt' }, as: :js
Knowing that this was a javascript request seemed logical to me. Unfortunately, I was met with the following error:
Failure/Error: @app.call(env)
ActionController::UnknownFormat:
MembershipsController#create is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []
Ok… So that message above tells us that the requested format was text/html. But
as: :js was specified since the template is an .js.erb.
Solution
Coming full circle we need to specify that the request being made is an AJAX or
XHR style request. Adding the option xhr: true solves this issue.
get users, params: { name: 'Geralt' }, xhr: true
Configure RSpec for multiple databases with Database Cleaner support
A Journey into Writing Union queries with Active Record
Join the conversation