Skip to content

Add support for Rails engines routes in request specs #2368

@drwl

Description

@drwl

Is your feature request related to a problem? Please describe.
When using the scaffold generator in Rails 6, rspec-rails will generate a request spec that uses url helpers. These generated specs work in a regular Rails application, but don't automatically work in Rails engines.

Rails engines tend to have routes that don't live inside Rails.application.routes. Instead, they are usually namespaced:

# Taken from https://guides.rubyonrails.org/engines.html#routes
Blorgh::Engine.routes.draw do
  resources :articles
end

Request specs are being pushed for, in favor of Controller specs [1]. Controller specs allow for route sets to be set [2] in specs, while Requests specs do not. (To me, it also looks like one is unable to set the route set for Feature and System specs as well.)

One is able to do:

module AppComponent
  RSpec.describe TeamsController, type: :controller do
    routes { AppComponent::Engine.routes }
    ...
# This works

But not able to do:

module AppComponent
  RSpec.describe "/teams", type: :request do
    routes { AppComponent::Engine.routes }
    ....
# => NoMethodError:
# =>  undefined method `routes' for RSpec::ExampleGroups::Teams:Class

[1] http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/
[2] https://github.com/rspec/rspec-rails/blob/main/lib/rspec/rails/example/controller_example_group.rb#L110

Describe the solution you'd like
Add the ability for the route set to be set in request specs, and possibly in feature and system specs as well.

Describe alternatives you've considered
One could not use url_helpers in request specs for Rails engines and instead hardcode the route urls (i.e. '/teams'), however, since url_helpers are used by the generators it seems like it would make sense to add support.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions