Skip to content

Instantly share code, notes, and snippets.

What would you like to do?

Requests specs x controller specs

Despite of they feel to be almost the same, there is a subtle difference between them: In request specs, you can exercise the full stack of a HTTP request (from routing to the view layer, so to speak).

Request specs also allows you to access more than one endpoint at a time, so you are able to test a whole scenario’s flow, whereas in controller specs you can exercise only one endpoint per test case (i.e., a single controller’s action).

# request spec
specify do
  get '/my-first/enpoint'
  post '/create-some-resource'
  get '/my-last/enpoint'

# controller spec
specify do
  get :new

Given the snippet above, one could then consider requests specs for integration-test purposes (where you strive for more collaboration), and controller specs for unit-test purposes (for more specialization).

It would also be reasonable to compare them to the parallel between feature (acceptance) specs x view specs, from a UI testing perspective.

When to use request specs?

Whenever you need to have some confidence level of a integration flow, mainly in the ones that are not aimed to human users (i.e., there will not be interactions from the browser), or there are some routing constraint involved (e.g., ENV variables dependency, subdomain rules, etc).

I also find that they’re more conceptually suited for testing JSON API responses (I can expand my arguments on this last sentence later).


This comment has been minimized.

Copy link

@georgeguimaraes georgeguimaraes commented Jul 27, 2015

Humm it seems to me you wanna blog about it!


This comment has been minimized.

Copy link

@igorffs igorffs commented Jul 27, 2015

👏 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.