What does specifying the type in an RSpec actually affect?

298 Views Asked by At

I have never been sure what the difference between these options are

RSpec.describe V2::DirectMessagesController, type: :controller

vs

RSpec.describe V2::DirectMessagesController, type: :request

Or where to even look to figure it out

1

There are 1 best solutions below

0
On

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).

A controller spec is an RSpec wrapper for a Rails functional test (ActionController::TestCase::Behavior). It allows you to simulate a single http request in each example, and then specify expected outcomes such as:

  • rendered templates
  • redirects
  • instance variables assigned in the controller to be shared with the view
  • cookies sent back with the response

Controller spec docs: https://relishapp.com/rspec/rspec-rails/docs/controller-specs

Request spec docs: https://relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec