Skip to content

Commit 277fe3e

Browse files
authored
Fix leaky modules in specs (#2504)
* Replace modules/classes declaration by anonymous modules/classes. * Rename validator spec with suffix validator_spec. Rubocop autocorrect * Fix RSpec/DescribeClass * Fix rubocop * Add CHANGELOG.md
1 parent 5b0066c commit 277fe3e

34 files changed

+1951
-2018
lines changed

.rubocop_todo.yml

+9-33
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2024-07-23 11:24:53 UTC using RuboCop version 1.64.1.
3+
# on 2024-10-06 16:00:59 UTC using RuboCop version 1.66.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
77
# versions of RuboCop, may require this file to be generated again.
88

9-
# Offense count: 1
10-
# Configuration parameters: AllowedMethods.
11-
# AllowedMethods: enums
12-
Lint/ConstantDefinitionInBlock:
13-
Exclude:
14-
- 'spec/grape/validations/validators/except_values_spec.rb'
15-
169
# Offense count: 1
1710
# Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns.
1811
Metrics/MethodLength:
@@ -29,17 +22,6 @@ Naming/VariableNumber:
2922
- 'spec/grape/exceptions/validation_errors_spec.rb'
3023
- 'spec/grape/validations_spec.rb'
3124

32-
# Offense count: 1
33-
# Configuration parameters: IgnoredMetadata.
34-
RSpec/DescribeClass:
35-
Exclude:
36-
- '**/spec/features/**/*'
37-
- '**/spec/requests/**/*'
38-
- '**/spec/routing/**/*'
39-
- '**/spec/system/**/*'
40-
- '**/spec/views/**/*'
41-
- 'spec/grape/named_api_spec.rb'
42-
4325
# Offense count: 2
4426
# This cop supports unsafe autocorrection (--autocorrect-all).
4527
# Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants.
@@ -68,7 +50,7 @@ RSpec/ExpectActual:
6850
# Offense count: 1
6951
RSpec/ExpectInHook:
7052
Exclude:
71-
- 'spec/grape/validations/validators/values_spec.rb'
53+
- 'spec/grape/validations/validators/values_validator_spec.rb'
7254

7355
# Offense count: 6
7456
# Configuration parameters: Max, AllowedIdentifiers, AllowedPatterns.
@@ -78,7 +60,7 @@ RSpec/IndexedLet:
7860
- 'spec/grape/presenters/presenter_spec.rb'
7961
- 'spec/shared/versioning_examples.rb'
8062

81-
# Offense count: 39
63+
# Offense count: 38
8264
# Configuration parameters: AssignmentOnly.
8365
RSpec/InstanceVariable:
8466
Exclude:
@@ -87,12 +69,6 @@ RSpec/InstanceVariable:
8769
- 'spec/grape/middleware/base_spec.rb'
8870
- 'spec/grape/middleware/versioner/accept_version_header_spec.rb'
8971
- 'spec/grape/middleware/versioner/header_spec.rb'
90-
- 'spec/grape/validations/validators/except_values_spec.rb'
91-
92-
# Offense count: 6
93-
RSpec/LeakyConstantDeclaration:
94-
Exclude:
95-
- 'spec/grape/validations/validators/except_values_spec.rb'
9672

9773
# Offense count: 1
9874
RSpec/MessageChain:
@@ -109,22 +85,22 @@ RSpec/RepeatedDescription:
10985
Exclude:
11086
- 'spec/grape/api_spec.rb'
11187
- 'spec/grape/endpoint_spec.rb'
112-
- 'spec/grape/validations/validators/allow_blank_spec.rb'
113-
- 'spec/grape/validations/validators/values_spec.rb'
88+
- 'spec/grape/validations/validators/allow_blank_validator_spec.rb'
89+
- 'spec/grape/validations/validators/values_validator_spec.rb'
11490

11591
# Offense count: 6
11692
RSpec/RepeatedExample:
11793
Exclude:
11894
- 'spec/grape/middleware/versioner/accept_version_header_spec.rb'
119-
- 'spec/grape/validations/validators/allow_blank_spec.rb'
95+
- 'spec/grape/validations/validators/allow_blank_validator_spec.rb'
12096

12197
# Offense count: 10
12298
RSpec/RepeatedExampleGroupDescription:
12399
Exclude:
124100
- 'spec/grape/api_spec.rb'
125101
- 'spec/grape/endpoint_spec.rb'
126102
- 'spec/grape/util/inheritable_setting_spec.rb'
127-
- 'spec/grape/validations/validators/values_spec.rb'
103+
- 'spec/grape/validations/validators/values_validator_spec.rb'
128104

129105
# Offense count: 4
130106
RSpec/StubbedMock:
@@ -133,7 +109,7 @@ RSpec/StubbedMock:
133109
- 'spec/grape/dsl/routing_spec.rb'
134110
- 'spec/grape/middleware/formatter_spec.rb'
135111

136-
# Offense count: 121
112+
# Offense count: 118
137113
RSpec/SubjectStub:
138114
Exclude:
139115
- 'spec/grape/api_spec.rb'
@@ -150,7 +126,7 @@ RSpec/SubjectStub:
150126
- 'spec/grape/middleware/globals_spec.rb'
151127
- 'spec/grape/middleware/stack_spec.rb'
152128

153-
# Offense count: 23
129+
# Offense count: 22
154130
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
155131
RSpec/VerifiedDoubles:
156132
Exclude:

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#### Fixes
1212

13+
* [#2504](https://github.com/ruby-grape/grape/pull/2504): Fix leaky modules in specs - [@ericproulx](https://github.com/ericproulx).
1314
* Your contribution here.
1415

1516
### 2.2.0 (2024-09-14)

spec/grape/api/deeply_included_options_spec.rb

+22-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
# frozen_string_literal: true
22

3-
module DeeplyIncludedOptionsSpec
4-
module Defaults
5-
extend ActiveSupport::Concern
6-
included do
7-
format :json
3+
describe Grape::API do
4+
let(:app) do
5+
main_api = api
6+
Class.new(Grape::API) do
7+
mount main_api
88
end
99
end
1010

11-
module Admin
12-
module Defaults
13-
extend ActiveSupport::Concern
14-
include DeeplyIncludedOptionsSpec::Defaults
15-
end
16-
17-
class Users < Grape::API
18-
include DeeplyIncludedOptionsSpec::Admin::Defaults
11+
let(:api) do
12+
deeply_included_options = options
13+
Class.new(Grape::API) do
14+
include deeply_included_options
1915

2016
resource :users do
2117
get do
@@ -25,16 +21,21 @@ class Users < Grape::API
2521
end
2622
end
2723

28-
class Main < Grape::API
29-
mount DeeplyIncludedOptionsSpec::Admin::Users
24+
let(:options) do
25+
deep_included_options_default = default
26+
Module.new do
27+
extend ActiveSupport::Concern
28+
include deep_included_options_default
29+
end
3030
end
31-
end
32-
33-
describe Grape::API do
34-
subject { DeeplyIncludedOptionsSpec::Main }
3531

36-
def app
37-
subject
32+
let(:default) do
33+
Module.new do
34+
extend ActiveSupport::Concern
35+
included do
36+
format :json
37+
end
38+
end
3839
end
3940

4041
it 'works for unspecified format' do

spec/grape/dsl/callbacks_spec.rb

+30-34
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
# frozen_string_literal: true
22

3-
module Grape
4-
module DSL
5-
module CallbacksSpec
6-
class Dummy
7-
include Grape::DSL::Callbacks
8-
end
9-
end
3+
describe Grape::DSL::Callbacks do
4+
subject { dummy_class }
105

11-
describe Callbacks do
12-
subject { Class.new(CallbacksSpec::Dummy) }
6+
let(:dummy_class) do
7+
Class.new do
8+
include Grape::DSL::Callbacks
9+
end
10+
end
1311

14-
let(:proc) { -> {} }
12+
let(:proc) { -> {} }
1513

16-
describe '.before' do
17-
it 'adds a block to "before"' do
18-
expect(subject).to receive(:namespace_stackable).with(:befores, proc)
19-
subject.before(&proc)
20-
end
21-
end
14+
describe '.before' do
15+
it 'adds a block to "before"' do
16+
expect(subject).to receive(:namespace_stackable).with(:befores, proc)
17+
subject.before(&proc)
18+
end
19+
end
2220

23-
describe '.before_validation' do
24-
it 'adds a block to "before_validation"' do
25-
expect(subject).to receive(:namespace_stackable).with(:before_validations, proc)
26-
subject.before_validation(&proc)
27-
end
28-
end
21+
describe '.before_validation' do
22+
it 'adds a block to "before_validation"' do
23+
expect(subject).to receive(:namespace_stackable).with(:before_validations, proc)
24+
subject.before_validation(&proc)
25+
end
26+
end
2927

30-
describe '.after_validation' do
31-
it 'adds a block to "after_validation"' do
32-
expect(subject).to receive(:namespace_stackable).with(:after_validations, proc)
33-
subject.after_validation(&proc)
34-
end
35-
end
28+
describe '.after_validation' do
29+
it 'adds a block to "after_validation"' do
30+
expect(subject).to receive(:namespace_stackable).with(:after_validations, proc)
31+
subject.after_validation(&proc)
32+
end
33+
end
3634

37-
describe '.after' do
38-
it 'adds a block to "after"' do
39-
expect(subject).to receive(:namespace_stackable).with(:afters, proc)
40-
subject.after(&proc)
41-
end
42-
end
35+
describe '.after' do
36+
it 'adds a block to "after"' do
37+
expect(subject).to receive(:namespace_stackable).with(:afters, proc)
38+
subject.after(&proc)
4339
end
4440
end
4541
end

spec/grape/dsl/headers_spec.rb

+45-48
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,59 @@
11
# frozen_string_literal: true
22

3-
module Grape
4-
module DSL
5-
module HeadersSpec
6-
class Dummy
7-
include Grape::DSL::Headers
8-
end
3+
describe Grape::DSL::Headers do
4+
subject { dummy_class.new }
5+
6+
let(:dummy_class) do
7+
Class.new do
8+
include Grape::DSL::Headers
99
end
10-
describe Headers do
11-
subject { HeadersSpec::Dummy.new }
10+
end
11+
12+
let(:header_data) do
13+
{ 'first key' => 'First Value',
14+
'second key' => 'Second Value' }
15+
end
16+
17+
context 'when headers are set' do
18+
describe '#header' do
19+
before do
20+
header_data.each { |k, v| subject.header(k, v) }
21+
end
1222

13-
let(:header_data) do
14-
{ 'first key' => 'First Value',
15-
'second key' => 'Second Value' }
23+
describe 'get' do
24+
it 'returns a specifc value' do
25+
expect(subject.header['first key']).to eq 'First Value'
26+
expect(subject.header['second key']).to eq 'Second Value'
27+
end
28+
29+
it 'returns all set headers' do
30+
expect(subject.header).to eq header_data
31+
expect(subject.headers).to eq header_data
32+
end
1633
end
1734

18-
context 'when headers are set' do
19-
describe '#header' do
20-
before do
21-
header_data.each { |k, v| subject.header(k, v) }
22-
end
23-
24-
describe 'get' do
25-
it 'returns a specifc value' do
26-
expect(subject.header['first key']).to eq 'First Value'
27-
expect(subject.header['second key']).to eq 'Second Value'
28-
end
29-
30-
it 'returns all set headers' do
31-
expect(subject.header).to eq header_data
32-
expect(subject.headers).to eq header_data
33-
end
34-
end
35-
36-
describe 'set' do
37-
it 'returns value' do
38-
expect(subject.header('third key', 'Third Value'))
39-
expect(subject.header['third key']).to eq 'Third Value'
40-
end
41-
end
42-
43-
describe 'delete' do
44-
it 'deletes a header key-value pair' do
45-
expect(subject.header('first key')).to eq header_data['first key']
46-
expect(subject.header).not_to have_key('first key')
47-
end
48-
end
35+
describe 'set' do
36+
it 'returns value' do
37+
expect(subject.header('third key', 'Third Value'))
38+
expect(subject.header['third key']).to eq 'Third Value'
4939
end
5040
end
5141

52-
context 'when no headers are set' do
53-
describe '#header' do
54-
it 'returns nil' do
55-
expect(subject.header['first key']).to be_nil
56-
expect(subject.header('first key')).to be_nil
57-
end
42+
describe 'delete' do
43+
it 'deletes a header key-value pair' do
44+
expect(subject.header('first key')).to eq header_data['first key']
45+
expect(subject.header).not_to have_key('first key')
5846
end
5947
end
6048
end
6149
end
50+
51+
context 'when no headers are set' do
52+
describe '#header' do
53+
it 'returns nil' do
54+
expect(subject.header['first key']).to be_nil
55+
expect(subject.header('first key')).to be_nil
56+
end
57+
end
58+
end
6259
end

0 commit comments

Comments
 (0)