In rspec-rails 8, how does a spec get its type, and what breaks when infer_spec_type_from_file_location! stays commented out?
answer
- metadata decides the mixed-in module
- type: :request includes RequestExampleGroup
- DIRECTORY_MAPPINGS: requests, integration, api
- commented out since rspec-rails 7.1
- explicit type wins over the folder
basics
~20 srspec-rails mixes helpers into an example group by its type: metadata. New rails_helper files leave infer_spec_type_from_file_location! commented out, so a spec in spec/requests without type: :request has no get or post and fails with NoMethodError.
solid answer
~40 srspec-rails registers one module per spec type with `config.include`, keyed on metadata: `type: :request` mixes in `RSpec::Rails::RequestExampleGroup` (the integration runner, so `get`, `post` and `response`), `type: :model` mixes in `ModelExampleGroup`, and so on for controller, view, helper, routing, mailer, job, system, channel and mailbox. `infer_spec_type_from_file_location!` adds derived metadata from the file path, so `spec/requests`, `spec/integration` and `spec/api` become `:request`, `spec/models` becomes `:model`. Since rspec-rails 7.1 the generated helper leaves that line commented and calls inference legacy. So in a new Rails 8.1 app, a file in `spec/requests` that omits `type: :request` fails on its first `get` with `NoMethodError`. Write the type explicitly, or uncomment the line; an explicit type always wins over the folder.
code
ruby · 18 lines# spec/requests/timesheets_spec.rb in a new Rails 8.1 app
require "rails_helper"
RSpec.describe "Timesheets" do # no type, inference commented out
it "shows a timesheet" do
get "/timesheets/1" # NoMethodError: undefined method 'get'
end
end
# Fix 1: explicit metadata
RSpec.describe "Timesheets", type: :request do
# get, post and response are now available
end
# Fix 2: in spec/rails_helper.rb
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
endgo deeper
Remember to write type: :request, type: :model and so on in each describe, and recognise a NoMethodError for get as missing metadata.
Explain how config.include with type metadata mixes in each example group and how infer_spec_type_from_file_location! derives the type from the folder.
Diagnose why identical spec files behave differently across two apps, and choose between explicit types and inference for a legacy suite.
Decide whether to standardise on explicit types across many repositories given the gem calls inference legacy, and how to enforce it without a disruptive mass edit.
## Types are metadata RSpec lets any example group carry **metadata**, a hash of keys and values attached in `describe`. rspec-rails uses one key, `:type`, to decide which Rails test behaviour an example group gets. When `rspec/rails` loads, it registers modules with `config.include`, each filtered by type: | Metadata | Module mixed in | What it gives the spec | |---|---|---| | `type: :model` | `RSpec::Rails::ModelExampleGroup` | Rails test lifecycle and fixtures | | `type: :request` | `RSpec::Rails::RequestExampleGroup` | `get`, `post`, `response`, `follow_redirect!` through the full stack | | `type: :controller` | `RSpec::Rails::ControllerExampleGroup` | `ActionController::TestCase` behaviour, `render_views` | | `type: :routing` | `RSpec::Rails::RoutingExampleGroup` | `route_to`, `be_routable` | | `type: :view` / `:helper` | `ViewExampleGroup` / `HelperExampleGroup` | `render`, `rendered`, `helper` | | `type: :mailer` / `:job` | `MailerExampleGroup` / `JobExampleGroup` | mailer and job spec support | | `type: :system` | `RSpec::Rails::SystemExampleGroup` | Capybara and `driven_by` | | `type: :channel` / `:mailbox` | `ChannelExampleGroup` / `MailboxExampleGroup` | Action Cable and Action Mailbox test helpers | `RSpec::Rails::Matchers`, which holds `have_http_status`, `have_enqueued_job`, `be_a_new` and the rest, is included in every group regardless of type. ## What inference does `config.infer_spec_type_from_file_location!` walks `RSpec::Rails::DIRECTORY_MAPPINGS` and defines **derived metadata** for each folder pattern. The important mappings: - `spec/models` to `:model`, `spec/controllers` to `:controller`, `spec/views` to `:view` - `spec/requests`, `spec/integration` and `spec/api` to `:request` - `spec/system` to `:system`, `spec/features` to `:feature` - `spec/jobs`, `spec/mailers`, `spec/channels`, `spec/mailboxes`, `spec/routing`, `spec/helpers` to their own types The derived block assigns with `||=`, so a `type:` written in the file always wins over the folder. A request spec kept in `spec/models` by mistake still behaves as a request spec when it says `type: :request`. ## The rspec-rails 7.1 change rspec-rails 7.1 commented out `infer_spec_type_from_file_location!` in newly generated `rails_helper.rb` files, and the template now says that inferring by location "is considered legacy and will be removed in a future version". Apps generated earlier keep the active line in their own helper, so two Rails 8.1 apps can behave differently with identical spec files. That difference is the usual source of the confusion. What breaks in a new app when a file omits the type: 1. A spec in `spec/requests/time_entries_spec.rb` declares `RSpec.describe "Time entries"` with no type. 2. No `RequestExampleGroup` is mixed in, because nothing set `:type`. 3. The first `get "/time_entries"` raises `NoMethodError`, since plain RSpec has no `get`. Model specs often seem to work without a type, because fixture support and the Rails matchers are also included globally, which hides the problem until a request, view or routing spec needs its own helpers. ## Writing it explicitly ```ruby RSpec.describe "Time entries API", type: :request do it "lists entries" do get "/time_entries", as: :json expect(response).to have_http_status(:ok) end end ``` The rspec-rails generators already write the type into every spec file they create, so explicit types are also what the tooling produces. ## Debugging a missing helper When a spec reports `NoMethodError` for a Rails test method, check in this order: 1. Does the outermost `describe` carry `type:`? Metadata set on a nested `context` only applies inside that context. 2. Is the type spelled as rspec-rails registers it, such as `:request` rather than `:requests`? 3. Is `infer_spec_type_from_file_location!` active in this app's `rails_helper.rb`, and is the file in a folder it maps? 4. Does the spec `require "rails_helper"`? Without it, rspec-rails never registers any of the modules. ## Choosing a policy - **Explicit types** read clearly in isolation and survive moving a file. They are the documented direction. - **Inference** saves a few characters per file and suits a large legacy suite that relied on it; uncomment the line rather than adding types to hundreds of files at once. - **Mixed suites** are fine, since the explicit value wins, but a linter rule or a review habit keeps new files consistent. Whichever you pick, a `NoMethodError` for `get`, `visit` or `render` in a new spec file almost always means the type metadata is missing.
- Which folders does inference map to type :request?`spec/requests`, `spec/integration` and `spec/api`. `RSpec::Rails::DIRECTORY_MAPPINGS` lists the request entry as a pattern matching any of the three, so an `api` folder of JSON endpoint specs gets the request helpers once inference is switched on.
- A spec in spec/models says type: :request and inference is on. Which type wins?The explicit `:request`. Inference defines derived metadata with `metadata[:type] ||= type`, so it only fills the key when the file did not set it. The spec gets `RequestExampleGroup` despite sitting in the models folder.
- A new Rails 8.1 app infers spec types from folders out of the box
- Inference overrides an explicit type: written in the file
- spec/api is not recognised as a request spec folder
- Without a type, a spec in spec/requests still gets get and post
Assumes rspec-rails 8.0 on Rails 8.1; rspec-rails 7.1 commented out infer_spec_type_from_file_location! in newly generated rails_helper.rb files, so older apps may still infer types by folder.