Skip to main content
  1. About
  2. Stack Internal
The 2026 Annual Developer Survey is live— take the Survey today!
Asked
Modified 7 years ago
Viewed 24k times
55

I'm working on a rails API and I'm now planning on writing some RSpec tests for the controllers. I've been reading around and I haven't been able to figure out what the actual difference between controller specs and request specs are and which one I should probably use if I'm testing an API.

2
  • 3
    If I were you, I'd forego controller specs in favor of request/integration specs.
    Sergio Tulentsev
    –  Sergio Tulentsev
    2016-11-28 19:11:23 +00:00
    Commented Nov 28, 2016 at 19:11
  • 2
    If anyone reads the above comment and wonders "why request/integration specs over controller specs?", I wrote about that here.
    Jason Swett
    –  Jason Swett
    2019-03-13 19:14:54 +00:00
    Commented Mar 13, 2019 at 19:14

3 Answers 3

54

Indeed, the RSpec team officially states controller specs are now obsolete.

http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/

For new Rails apps: we don't recommend adding the rails-controller-testing gem to your application. The official recommendation of the Rails team and the RSpec core team is to write request specs instead. Request specs allow you to focus on a single controller action, but unlike controller tests involve the router, the middleware stack, and both rack requests and responses. This adds realism to the test that you are writing, and helps avoid many of the issues that are common in controller specs. In Rails 5, request specs are significantly faster than either request or controller specs were in rails 4, thanks to the work by Eileen Uchitelle1 of the Rails Committer Team.

Sign up to request clarification or add additional context in comments.

Comments

36

Rails 3 & 4

Controller specs - A controller spec is an RSpec wrapper for a Rails functional test. It allows you to simulate a single http request in each example, and then specify expected outcomes

Request specs - Request specs provide a thin wrapper around Rails' integration tests, and are designed to drive behavior through the full stack, including routing (provided by Rails) and without stubbing (that's up to you).

So if you want to test API controllers I would recommend to use Controller specs as you are testing single requests.


Rails 5+

Rails 5 improved the speed and realism of request specs over Rails version 4's controller and request specs. The official recommendation of the Rails team and the RSpec core team is to write request specs instead (of controller specs).

2 Comments

If you're using older versions of Rails, then the general recommended code styles seem to be towards controller specs. Rails 5 has changed how controller tests work; they're integration tests and you can no longer access the internals of the controller. It's now not clear to me if there is any point in having controller tests at all. Request and Feature specs seem to be the recommended approach for future development. everydayrails.com/2016/09/05/…
I'm having an issue about whether or not to unit test controllers by just calling the action directly and not using the http layer. Is this recommended?
10

Quoting Aaron Sumner:

Both the Rails and RSpec teams suggest replacing or removing your app’s controller tests (also known as the functional test layer), in favor of directly testing models (units), or with higher-level integration tests.

For a new API, I prefer to use request spec and hit my "end points" (aka Models and Business logic) than using controller tests.

Comments

Your Answer

Reminder: Answers generated by AI tools are not allowed due to Stack Overflow's artificial intelligence policy

Draft saved
Draft discarded

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

lang-rb
Morty Proxy This is a proxified and sanitized view of the page, visit original site.