rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

Cop idea: RSpecRails/RequestUrlHelper

Open phil-workato opened this issue 2 years ago • 0 comments

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)

One confirmation that the use of non-generated paths is preferred (with a single edge case exception) is in rspec-rails examples:

post "/widgets", :params => { :widget => {:name => "My Widget"} }, :headers => headers

Another is Rails testing guides:

post "/articles",
    params: { article: { title: "can create", body: "article successfully." } }

A common way is to use the path helpers:

expect {
  post widgets_path, params: {:widget => {:name => "My Widget"}}, :headers => headers
}.to change { ... }

Rails guides mention that:

[Generating Paths and URLs from Code] ... reduces the brittleness of your view

however, in specs it is the other way around. To keep the public API paths intact, you must use direct paths in request specs, or write additional routing specs.

Suggestion

Add a cop that would flag usages of path/URL helpers in request specs.

cc @koic WDYT?

phil-workato avatar Oct 26 '23 12:10 phil-workato