rubocop-rspec 1.5.2 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/config/default.yml +4 -0
- data/lib/rubocop-rspec.rb +1 -0
- data/lib/rubocop/cop/rspec/named_subject.rb +67 -0
- data/lib/rubocop/rspec/version.rb +1 -1
- data/rubocop-rspec.gemspec +3 -0
- data/spec/expect_violation/expectation_spec.rb +85 -0
- data/spec/rubocop/cop/rspec/any_instance_spec.rb +18 -36
- data/spec/rubocop/cop/rspec/describe_class_spec.rb +65 -62
- data/spec/rubocop/cop/rspec/describe_method_spec.rb +17 -18
- data/spec/rubocop/cop/rspec/described_class_spec.rb +89 -139
- data/spec/rubocop/cop/rspec/example_wording_spec.rb +20 -29
- data/spec/rubocop/cop/rspec/focus_spec.rb +115 -63
- data/spec/rubocop/cop/rspec/instance_variable_spec.rb +23 -35
- data/spec/rubocop/cop/rspec/multiple_describes_spec.rb +16 -31
- data/spec/rubocop/cop/rspec/named_subject_spec.rb +62 -0
- data/spec/rubocop/cop/rspec/not_to_not_spec.rb +14 -16
- data/spec/rubocop/cop/rspec/verified_doubles_spec.rb +39 -43
- data/spec/spec_helper.rb +5 -0
- data/spec/support/expect_violation.rb +166 -0
- metadata +51 -2
@@ -2,32 +2,31 @@ describe RuboCop::Cop::RSpec::DescribeMethod do
|
|
2
2
|
subject(:cop) { described_class.new }
|
3
3
|
|
4
4
|
it 'ignores describes with only a class' do
|
5
|
-
|
6
|
-
expect(cop.offenses.empty?).to be(true)
|
5
|
+
expect_no_violations('describe Some::Class do; end')
|
7
6
|
end
|
8
7
|
|
9
8
|
it 'enforces non-method names' do
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
expect(cop.offenses.size).to eq(1)
|
16
|
-
expect(cop.offenses.map(&:line).sort).to eq([1])
|
17
|
-
expect(cop.messages)
|
18
|
-
.to eq(['The second argument to describe should be the method being ' \
|
19
|
-
"tested. '#instance' or '.class'"])
|
9
|
+
expect_violation(<<-RUBY)
|
10
|
+
describe Some::Class, 'nope', '.incorrect_usage' do
|
11
|
+
^^^^^^ The second argument to describe should be the method being tested. '#instance' or '.class'
|
12
|
+
end
|
13
|
+
RUBY
|
20
14
|
end
|
21
15
|
|
22
16
|
it 'skips methods starting with a . or #' do
|
23
|
-
|
24
|
-
|
25
|
-
|
17
|
+
expect_no_violations(<<-RUBY)
|
18
|
+
describe Some::Class, '.asdf' do
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Some::Class, '#fdsa' do
|
22
|
+
end
|
23
|
+
RUBY
|
26
24
|
end
|
27
25
|
|
28
26
|
it 'skips specs not having a string second argument' do
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
expect_no_violations(<<-RUBY)
|
28
|
+
describe Some::Class, :config do
|
29
|
+
end
|
30
|
+
RUBY
|
32
31
|
end
|
33
32
|
end
|
@@ -2,176 +2,126 @@ describe RuboCop::Cop::RSpec::DescribedClass do
|
|
2
2
|
subject(:cop) { described_class.new }
|
3
3
|
|
4
4
|
it 'checks for the use of the described class' do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
expect(cop.messages)
|
18
|
-
.to eq(['Use `described_class` instead of `MyClass`'] * 3)
|
19
|
-
expect(cop.highlights).to eq(['MyClass'] * 3)
|
5
|
+
expect_violation(<<-RUBY)
|
6
|
+
describe MyClass do
|
7
|
+
include MyClass
|
8
|
+
^^^^^^^ Use `described_class` instead of `MyClass`
|
9
|
+
|
10
|
+
subject { MyClass.do_something }
|
11
|
+
^^^^^^^ Use `described_class` instead of `MyClass`
|
12
|
+
|
13
|
+
before { MyClass.do_something }
|
14
|
+
^^^^^^^ Use `described_class` instead of `MyClass`
|
15
|
+
end
|
16
|
+
RUBY
|
20
17
|
end
|
21
18
|
|
22
19
|
it 'ignores described class as string' do
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
'end'
|
29
|
-
]
|
30
|
-
)
|
31
|
-
expect(cop.offenses).to be_empty
|
20
|
+
expect_no_violations(<<-RUBY)
|
21
|
+
describe MyClass do
|
22
|
+
subject { "MyClass" }
|
23
|
+
end
|
24
|
+
RUBY
|
32
25
|
end
|
33
26
|
|
34
27
|
it 'ignores describe that do not referece to a class' do
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
'end'
|
41
|
-
]
|
42
|
-
)
|
43
|
-
expect(cop.offenses).to be_empty
|
28
|
+
expect_no_violations(<<-RUBY)
|
29
|
+
describe "MyClass" do
|
30
|
+
subject { "MyClass" }
|
31
|
+
end
|
32
|
+
RUBY
|
44
33
|
end
|
45
34
|
|
46
35
|
it 'ignores class if the scope is changing' do
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
)
|
63
|
-
expect(cop.offenses).to be_empty
|
36
|
+
expect_no_violations(<<-RUBY)
|
37
|
+
describe MyClass do
|
38
|
+
def method
|
39
|
+
include MyClass
|
40
|
+
end
|
41
|
+
|
42
|
+
class OtherClass
|
43
|
+
include MyClass
|
44
|
+
end
|
45
|
+
|
46
|
+
module MyModle
|
47
|
+
include MyClass
|
48
|
+
end
|
49
|
+
end
|
50
|
+
RUBY
|
64
51
|
end
|
65
52
|
|
66
53
|
it 'only takes class from top level describes' do
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
)
|
78
|
-
expect(cop.offenses.size).to eq(1)
|
79
|
-
expect(cop.offenses.map(&:line).sort).to eq([4])
|
80
|
-
expect(cop.messages)
|
81
|
-
.to eq(['Use `described_class` instead of `MyClass`'])
|
82
|
-
expect(cop.highlights).to eq(['MyClass'])
|
54
|
+
expect_violation(<<-RUBY)
|
55
|
+
describe MyClass do
|
56
|
+
describe MyClass::Foo do
|
57
|
+
subject { MyClass::Foo }
|
58
|
+
|
59
|
+
let(:foo) { MyClass }
|
60
|
+
^^^^^^^ Use `described_class` instead of `MyClass`
|
61
|
+
end
|
62
|
+
end
|
63
|
+
RUBY
|
83
64
|
end
|
84
65
|
|
85
66
|
it 'ignores subclasses' do
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
'end'
|
92
|
-
]
|
93
|
-
)
|
94
|
-
expect(cop.offenses).to be_empty
|
67
|
+
expect_no_violations(<<-RUBY)
|
68
|
+
describe MyClass do
|
69
|
+
subject { MyClass::SubClass }
|
70
|
+
end
|
71
|
+
RUBY
|
95
72
|
end
|
96
73
|
|
97
74
|
it 'ignores if namespace is not matching' do
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
'end'
|
105
|
-
]
|
106
|
-
)
|
107
|
-
expect(cop.offenses).to be_empty
|
75
|
+
expect_no_violations(<<-RUBY)
|
76
|
+
describe MyNamespace::MyClass do
|
77
|
+
subject { ::MyClass }
|
78
|
+
let(:foo) { MyClass }
|
79
|
+
end
|
80
|
+
RUBY
|
108
81
|
end
|
109
82
|
|
110
83
|
it 'checks for the use of described class with namespace' do
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
]
|
118
|
-
)
|
119
|
-
expect(cop.offenses.size).to eq(1)
|
120
|
-
expect(cop.offenses.map(&:line).sort).to eq([2])
|
121
|
-
expect(cop.messages)
|
122
|
-
.to eq(['Use `described_class` instead of `MyNamespace::MyClass`'])
|
123
|
-
expect(cop.highlights).to eq(['MyNamespace::MyClass'])
|
84
|
+
expect_violation(<<-RUBY)
|
85
|
+
describe MyNamespace::MyClass do
|
86
|
+
subject { MyNamespace::MyClass }
|
87
|
+
^^^^^^^^^^^^^^^^^^^^ Use `described_class` instead of `MyNamespace::MyClass`
|
88
|
+
end
|
89
|
+
RUBY
|
124
90
|
end
|
125
91
|
|
126
92
|
it 'does not flag violations within a scope change' do
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
'end'
|
137
|
-
]
|
138
|
-
)
|
139
|
-
|
140
|
-
expect(cop.offenses).to be_empty
|
93
|
+
expect_no_violations(<<-RUBY)
|
94
|
+
describe MyNamespace::MyClass do
|
95
|
+
before do
|
96
|
+
class Foo
|
97
|
+
thing = MyNamespace::MyClass.new
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
RUBY
|
141
102
|
end
|
142
103
|
|
143
104
|
it 'does not flag violations within a scope change' do
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
'end'
|
152
|
-
]
|
153
|
-
)
|
154
|
-
|
155
|
-
expect(cop.offenses).to be_empty
|
105
|
+
expect_no_violations(<<-RUBY)
|
106
|
+
describe do
|
107
|
+
before do
|
108
|
+
MyNamespace::MyClass.new
|
109
|
+
end
|
110
|
+
end
|
111
|
+
RUBY
|
156
112
|
end
|
157
113
|
|
158
114
|
it 'checks for the use of described class with module' do
|
159
115
|
skip
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
)
|
170
|
-
expect(cop.offenses.size).to eq(1)
|
171
|
-
expect(cop.offenses.map(&:line).sort).to eq([2])
|
172
|
-
expect(cop.messages)
|
173
|
-
.to eq(['Use `described_class` instead of `MyNamespace::MyClass`'])
|
174
|
-
expect(cop.highlights).to eq(['MyNamespace::MyClass'])
|
116
|
+
|
117
|
+
expect_violation(<<-RUBY)
|
118
|
+
module MyNamespace
|
119
|
+
describe MyClass do
|
120
|
+
subject { MyNamespace::MyClass }
|
121
|
+
^^^^^^^^^^^^^^^^^^^^ Use `described_class` instead of `MyNamespace::MyClass`
|
122
|
+
end
|
123
|
+
end
|
124
|
+
RUBY
|
175
125
|
end
|
176
126
|
|
177
127
|
it 'autocorrects an offenses' do
|
@@ -10,47 +10,38 @@ describe RuboCop::Cop::RSpec::ExampleWording, :config do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'ignores non-example blocks' do
|
13
|
-
|
14
|
-
expect(cop.offenses).to be_empty
|
13
|
+
expect_no_violations('foo "should do something" do; end')
|
15
14
|
end
|
16
15
|
|
17
16
|
it 'finds description with `should` at the beginning' do
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
expect(cop.highlights).to eq(['should do something'])
|
17
|
+
expect_violation(<<-RUBY)
|
18
|
+
it 'should do something' do
|
19
|
+
^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
|
20
|
+
end
|
21
|
+
RUBY
|
24
22
|
end
|
25
23
|
|
26
24
|
it 'finds description with `Should` at the beginning' do
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
expect(cop.highlights).to eq(['Should do something'])
|
25
|
+
expect_violation(<<-RUBY)
|
26
|
+
it 'Should do something' do
|
27
|
+
^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
|
28
|
+
end
|
29
|
+
RUBY
|
33
30
|
end
|
34
31
|
|
35
32
|
it 'finds description with `shouldn\'t` at the beginning' do
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
expect(cop.highlights).to eq(['shouldn\'t do something'])
|
33
|
+
expect_violation(<<-RUBY)
|
34
|
+
it "shouldn't do something" do
|
35
|
+
^^^^^^^^^^^^^^^^^^^^^^ Do not use should when describing your tests.
|
36
|
+
end
|
37
|
+
RUBY
|
42
38
|
end
|
43
39
|
|
44
40
|
it 'skips descriptions without `should` at the beginning' do
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
" 'here' do",
|
50
|
-
'end'
|
51
|
-
]
|
52
|
-
)
|
53
|
-
expect(cop.offenses).to be_empty
|
41
|
+
expect_no_violations(<<-RUBY)
|
42
|
+
it 'finds no should here' do
|
43
|
+
end
|
44
|
+
RUBY
|
54
45
|
end
|
55
46
|
|
56
47
|
it 'corrects `it "should only have"` to it "only has"' do
|
@@ -1,78 +1,130 @@
|
|
1
1
|
describe RuboCop::Cop::RSpec::Focus do
|
2
2
|
subject(:cop) { described_class.new }
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
4
|
+
# rubocop:disable RSpec/ExampleLength
|
5
|
+
it 'flags all rspec example blocks with that include `focus: true`' do
|
6
|
+
expect_violation(<<-RUBY)
|
7
|
+
example 'test', meta: true, focus: true do; end
|
8
|
+
^^^^^^^^^^^ Focused spec found.
|
9
|
+
xit 'test', meta: true, focus: true do; end
|
10
|
+
^^^^^^^^^^^ Focused spec found.
|
11
|
+
describe 'test', meta: true, focus: true do; end
|
12
|
+
^^^^^^^^^^^ Focused spec found.
|
13
|
+
it 'test', meta: true, focus: true do; end
|
14
|
+
^^^^^^^^^^^ Focused spec found.
|
15
|
+
xspecify 'test', meta: true, focus: true do; end
|
16
|
+
^^^^^^^^^^^ Focused spec found.
|
17
|
+
specify 'test', meta: true, focus: true do; end
|
18
|
+
^^^^^^^^^^^ Focused spec found.
|
19
|
+
example_group 'test', meta: true, focus: true do; end
|
20
|
+
^^^^^^^^^^^ Focused spec found.
|
21
|
+
scenario 'test', meta: true, focus: true do; end
|
22
|
+
^^^^^^^^^^^ Focused spec found.
|
23
|
+
xexample 'test', meta: true, focus: true do; end
|
24
|
+
^^^^^^^^^^^ Focused spec found.
|
25
|
+
xdescribe 'test', meta: true, focus: true do; end
|
26
|
+
^^^^^^^^^^^ Focused spec found.
|
27
|
+
context 'test', meta: true, focus: true do; end
|
28
|
+
^^^^^^^^^^^ Focused spec found.
|
29
|
+
xcontext 'test', meta: true, focus: true do; end
|
30
|
+
^^^^^^^^^^^ Focused spec found.
|
31
|
+
feature 'test', meta: true, focus: true do; end
|
32
|
+
^^^^^^^^^^^ Focused spec found.
|
33
|
+
xfeature 'test', meta: true, focus: true do; end
|
34
|
+
^^^^^^^^^^^ Focused spec found.
|
35
|
+
xscenario 'test', meta: true, focus: true do; end
|
36
|
+
^^^^^^^^^^^ Focused spec found.
|
37
|
+
RUBY
|
38
|
+
end
|
22
39
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
40
|
+
it 'flags all repec example blocks that include `:focus`' do
|
41
|
+
expect_violation(<<-RUBY)
|
42
|
+
example_group 'test', :focus do; end
|
43
|
+
^^^^^^ Focused spec found.
|
44
|
+
feature 'test', :focus do; end
|
45
|
+
^^^^^^ Focused spec found.
|
46
|
+
xexample 'test', :focus do; end
|
47
|
+
^^^^^^ Focused spec found.
|
48
|
+
xdescribe 'test', :focus do; end
|
49
|
+
^^^^^^ Focused spec found.
|
50
|
+
xscenario 'test', :focus do; end
|
51
|
+
^^^^^^ Focused spec found.
|
52
|
+
specify 'test', :focus do; end
|
53
|
+
^^^^^^ Focused spec found.
|
54
|
+
example 'test', :focus do; end
|
55
|
+
^^^^^^ Focused spec found.
|
56
|
+
xfeature 'test', :focus do; end
|
57
|
+
^^^^^^ Focused spec found.
|
58
|
+
xspecify 'test', :focus do; end
|
59
|
+
^^^^^^ Focused spec found.
|
60
|
+
scenario 'test', :focus do; end
|
61
|
+
^^^^^^ Focused spec found.
|
62
|
+
describe 'test', :focus do; end
|
63
|
+
^^^^^^ Focused spec found.
|
64
|
+
xit 'test', :focus do; end
|
65
|
+
^^^^^^ Focused spec found.
|
66
|
+
context 'test', :focus do; end
|
67
|
+
^^^^^^ Focused spec found.
|
68
|
+
xcontext 'test', :focus do; end
|
69
|
+
^^^^^^ Focused spec found.
|
70
|
+
it 'test', :focus do; end
|
71
|
+
^^^^^^ Focused spec found.
|
72
|
+
RUBY
|
73
|
+
end
|
36
74
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
75
|
+
it 'does not flag unfocused specs' do
|
76
|
+
expect_no_violations(<<-RUBY)
|
77
|
+
xcontext 'test' do; end
|
78
|
+
xscenario 'test' do; end
|
79
|
+
xspecify 'test' do; end
|
80
|
+
describe 'test' do; end
|
81
|
+
example 'test' do; end
|
82
|
+
xexample 'test' do; end
|
83
|
+
scenario 'test' do; end
|
84
|
+
specify 'test' do; end
|
85
|
+
xit 'test' do; end
|
86
|
+
feature 'test' do; end
|
87
|
+
xfeature 'test' do; end
|
88
|
+
context 'test' do; end
|
89
|
+
it 'test' do; end
|
90
|
+
example_group 'test' do; end
|
91
|
+
xdescribe 'test' do; end
|
92
|
+
RUBY
|
47
93
|
end
|
48
94
|
|
49
95
|
it 'does not flag a method that is focused twice' do
|
50
|
-
|
51
|
-
|
96
|
+
expect_violation(<<-RUBY)
|
97
|
+
fit "foo", :focus do
|
98
|
+
^^^^^^^^^^^^^^^^^ Focused spec found.
|
99
|
+
end
|
100
|
+
RUBY
|
52
101
|
end
|
53
102
|
|
54
103
|
it 'ignores non-rspec code with :focus blocks' do
|
55
|
-
|
56
|
-
|
104
|
+
expect_no_violations(<<-RUBY)
|
105
|
+
some_method "foo", focus: true do
|
106
|
+
end
|
107
|
+
RUBY
|
57
108
|
end
|
58
109
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
110
|
+
it 'flags focused block types' do
|
111
|
+
expect_violation(<<-RUBY)
|
112
|
+
fdescribe 'test' do; end
|
113
|
+
^^^^^^^^^^^^^^^^ Focused spec found.
|
114
|
+
ffeature 'test' do; end
|
115
|
+
^^^^^^^^^^^^^^^ Focused spec found.
|
116
|
+
fcontext 'test' do; end
|
117
|
+
^^^^^^^^^^^^^^^ Focused spec found.
|
118
|
+
fit 'test' do; end
|
119
|
+
^^^^^^^^^^ Focused spec found.
|
120
|
+
fscenario 'test' do; end
|
121
|
+
^^^^^^^^^^^^^^^^ Focused spec found.
|
122
|
+
fexample 'test' do; end
|
123
|
+
^^^^^^^^^^^^^^^ Focused spec found.
|
124
|
+
fspecify 'test' do; end
|
125
|
+
^^^^^^^^^^^^^^^ Focused spec found.
|
126
|
+
focus 'test' do; end
|
127
|
+
^^^^^^^^^^^^ Focused spec found.
|
128
|
+
RUBY
|
77
129
|
end
|
78
130
|
end
|