rubocop-rspec: RSpec/ScatteredSetup: false positive

I am using rails 6 + rspec + rswag and have a following repeating pattern in my request specs:

  path '/clusters/{id}/delete' do
    post 'Delete cluster' do
      tags 'Cluster'
      operationId 'deleteCluster'
      produces 'application/json'
      parameter name: :id, in: :path, type: :integer

      response '200', 'success' do
        before do
          cleanup_operations(existing_cluster)
        end

        schema '$ref' => '#/components/schemas/OperationResult'
        run_test!
      end

      response '409', 'conflict' do
        schema '$ref' => '#/components/schemas/OperationResult'
        run_test!
      end
    end

The idea is that model does not allow concurrent operations, so if there are running ones it will return 409 code. But I also want to test 200 response, so I use a cleanup helper, which forcedly marks all operations as finished. This would be fine, but I have several similar endpoints to test, e.g. start, stop, etc. And for some responses I need before callback and for some of them I do not, thus I cannot declare before on the describe level.

However, RSpec/ScatteredSetup says:

spec/requests/api_controller/clusters_spec.rb:147:9: C: RSpec/ScatteredSetup: Do not define multiple before hooks in the same example group (also defined on lines 182, 215, 377, 444, 485, 546, 604, 635).
        before do ...
        ^^^^^^^^^
spec/requests/api_controller/clusters_spec.rb:182:9: C: RSpec/ScatteredSetup: Do not define multiple before hooks in the same example group (also defined on lines 147, 215, 377, 444, 485, 546, 604, 635).
        before do ...
        ^^^^^^^^^
spec/requests/api_controller/clusters_spec.rb:215:9: C: RSpec/ScatteredSetup: Do not define multiple before hooks in the same example group (also defined on lines 147, 182, 377, 444, 485, 546, 604, 635).
        before do ...
        ^^^^^^^^^
spec/requests/api_controller/clusters_spec.rb:377:9: C: RSpec/ScatteredSetup: Do not define multiple before hooks in the same example group (also defined on lines 147, 182, 215, 444, 485, 546, 604, 635).
        before do ...

and so on.

And I cannot obey, because it will ruin my test logic. Am I missing something?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Thanks for your previous comment, now I use:

RSpec:
  Language:
    ExampleGroups:
      Regular:
        - path
        - post
        - put
        - get
        - delete
        - response
    Examples:
      Regular:
        - run_test!

so far so good. I only receive reasonable warnings.

I have not prepared a PR yet, but I found a closed issue https://github.com/rswag/rswag/issues/138. And it seems that they are not really willing to accept something like that. Although I want to try anyway

@pirj, yeah, I think so. Thanks for responses, that helped me a lot. I’m closing this one