Skip to content

Commit 4c4e4fa

Browse files
rrosenblumkoic
authored andcommitted
Make use of expect_correction in rails tests
1 parent 6fb055b commit 4c4e4fa

19 files changed

+497
-573
lines changed

spec/rubocop/cop/rails/active_record_aliases_spec.rb

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,67 +4,53 @@
44
subject(:cop) { described_class.new }
55

66
describe '#update_attributes' do
7-
it 'registers an offense' do
7+
it 'registers an offense and corrects' do
88
expect_offense(<<-RUBY.strip_indent)
99
book.update_attributes(author: "Alice")
1010
^^^^^^^^^^^^^^^^^ Use `update` instead of `update_attributes`.
1111
RUBY
12-
end
1312

14-
it 'is autocorrected' do
15-
new_source = autocorrect_source(
16-
'book.update_attributes(author: "Alice")'
17-
)
18-
expect(new_source).to eq 'book.update(author: "Alice")'
13+
expect_correction(<<-RUBY.strip_indent)
14+
book.update(author: "Alice")
15+
RUBY
1916
end
2017
end
2118

2219
describe '#update_attributes!' do
23-
it 'registers an offense' do
20+
it 'registers an offense and corrects' do
2421
expect_offense(<<-RUBY.strip_indent)
2522
book.update_attributes!(author: "Bob")
2623
^^^^^^^^^^^^^^^^^^ Use `update!` instead of `update_attributes!`.
2724
RUBY
28-
end
2925

30-
it 'is autocorrected' do
31-
new_source = autocorrect_source(
32-
'book.update_attributes!(author: "Bob")'
33-
)
34-
expect(new_source).to eq 'book.update!(author: "Bob")'
26+
expect_correction(<<-RUBY.strip_indent)
27+
book.update!(author: "Bob")
28+
RUBY
3529
end
3630
end
3731

3832
describe '#update' do
3933
it 'does not register an offense' do
4034
expect_no_offenses('book.update(author: "Alice")')
4135
end
42-
43-
it 'is not autocorrected' do
44-
source = 'book.update(author: "Alice")'
45-
new_source = autocorrect_source(source)
46-
expect(new_source).to eq source
47-
end
4836
end
4937

5038
describe '#update!' do
5139
it 'does not register an offense' do
5240
expect_no_offenses('book.update!(author: "Bob")')
5341
end
54-
55-
it 'is not autocorrected' do
56-
source = 'book.update!(author: "Bob")'
57-
new_source = autocorrect_source(source)
58-
expect(new_source).to eq source
59-
end
6042
end
6143

6244
describe 'other use of the `update_attributes` string' do
6345
it 'does not autocorrect the other usage' do
64-
new_source = autocorrect_source(
65-
'update_attributes_book.update_attributes(author: "Alice")'
66-
)
67-
expect(new_source).to eq 'update_attributes_book.update(author: "Alice")'
46+
expect_offense(<<-RUBY.strip_indent)
47+
update_attributes_book.update_attributes(author: "Alice")
48+
^^^^^^^^^^^^^^^^^ Use `update` instead of `update_attributes`.
49+
RUBY
50+
51+
expect_correction(<<-RUBY.strip_indent)
52+
update_attributes_book.update(author: "Alice")
53+
RUBY
6854
end
6955
end
7056
end

spec/rubocop/cop/rails/active_support_aliases_spec.rb

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55

66
describe 'String' do
77
describe '#starts_with?' do
8-
it 'is registered as an offense' do
8+
it 'registers as an offense and corrects' do
99
expect_offense(<<-RUBY.strip_indent)
1010
'some_string'.starts_with?('prefix')
1111
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `start_with?` instead of `starts_with?`.
1212
RUBY
13-
end
1413

15-
it 'is autocorrected' do
16-
new_source = autocorrect_source(
17-
"'some_string'.starts_with?('prefix')"
18-
)
19-
expect(new_source).to eq "'some_string'.start_with?('prefix')"
14+
expect_correction(<<-RUBY.strip_indent)
15+
'some_string'.start_with?('prefix')
16+
RUBY
2017
end
2118
end
2219

@@ -27,18 +24,15 @@
2724
end
2825

2926
describe '#ends_with?' do
30-
it 'is registered as an offense' do
27+
it 'registers as an offense and corrects' do
3128
expect_offense(<<-RUBY.strip_indent)
3229
'some_string'.ends_with?('prefix')
3330
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `end_with?` instead of `ends_with?`.
3431
RUBY
35-
end
3632

37-
it 'is autocorrected' do
38-
new_source = autocorrect_source(
39-
"'some_string'.ends_with?('prefix')"
40-
)
41-
expect(new_source).to eq "'some_string'.end_with?('prefix')"
33+
expect_correction(<<-RUBY.strip_indent)
34+
'some_string'.end_with?('prefix')
35+
RUBY
4236
end
4337
end
4438

@@ -51,17 +45,15 @@
5145

5246
describe 'Array' do
5347
describe '#append' do
54-
it 'is registered as an offense' do
48+
it 'registers as an offense and does not correct' do
5549
expect_offense(<<-RUBY.strip_indent)
5650
[1, 'a', 3].append('element')
5751
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `<<` instead of `append`.
5852
RUBY
59-
end
6053

61-
it 'is not autocorrected' do
62-
source = "[1, 'a', 3].append('element')"
63-
new_source = autocorrect_source(source)
64-
expect(new_source).to eq source
54+
expect_correction(<<-RUBY.strip_indent)
55+
[1, 'a', 3].append('element')
56+
RUBY
6557
end
6658
end
6759

@@ -72,18 +64,15 @@
7264
end
7365

7466
describe '#prepend' do
75-
it 'is registered as an offense' do
67+
it 'registers as an offense and corrects' do
7668
expect_offense(<<-RUBY.strip_indent)
7769
[1, 'a', 3].prepend('element')
7870
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `unshift` instead of `prepend`.
7971
RUBY
80-
end
8172

82-
it 'is autocorrected' do
83-
new_source = autocorrect_source(
84-
"[1, 'a', 3].prepend('element')"
85-
)
86-
expect(new_source).to eq "[1, 'a', 3].unshift('element')"
73+
expect_correction(<<-RUBY.strip_indent)
74+
[1, 'a', 3].unshift('element')
75+
RUBY
8776
end
8877
end
8978

spec/rubocop/cop/rails/application_job_spec.rb

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ class ApplicationJob < ActiveJob::Base; end
7070
class MyJob < ActiveJob::Base; end
7171
^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`.
7272
RUBY
73-
end
7473

75-
it 'auto-corrects' do
76-
expect(autocorrect_source('class MyJob < ActiveJob::Base; end'))
77-
.to eq('class MyJob < ApplicationJob; end')
74+
expect_correction(<<-RUBY.strip_indent)
75+
class MyJob < ApplicationJob; end
76+
RUBY
7877
end
7978
end
8079

@@ -99,30 +98,36 @@ class Nested::MyJob < ActiveJob::Base; end
9998
end
10099

101100
it 'corrects jobs defined using Class.new' do
102-
source = 'MyJob = Class.new(ActiveJob::Base)'
103-
inspect_source(source)
104-
expect(cop.messages).to eq(['Jobs should subclass `ApplicationJob`.'])
105-
expect(cop.highlights).to eq(['ActiveJob::Base'])
106-
expect(autocorrect_source(source))
107-
.to eq('MyJob = Class.new(ApplicationJob)')
101+
expect_offense(<<-RUBY.strip_indent)
102+
MyJob = Class.new(ActiveJob::Base)
103+
^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`.
104+
RUBY
105+
106+
expect_correction(<<-RUBY.strip_indent)
107+
MyJob = Class.new(ApplicationJob)
108+
RUBY
108109
end
109110

110111
it 'corrects nested jobs defined using Class.new' do
111-
source = 'Nested::MyJob = Class.new(ActiveJob::Base)'
112-
inspect_source(source)
113-
expect(cop.messages).to eq(['Jobs should subclass `ApplicationJob`.'])
114-
expect(cop.highlights).to eq(['ActiveJob::Base'])
115-
expect(autocorrect_source(source))
116-
.to eq('Nested::MyJob = Class.new(ApplicationJob)')
112+
expect_offense(<<-RUBY.strip_indent)
113+
Nested::MyJob = Class.new(ActiveJob::Base)
114+
^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`.
115+
RUBY
116+
117+
expect_correction(<<-RUBY.strip_indent)
118+
Nested::MyJob = Class.new(ApplicationJob)
119+
RUBY
117120
end
118121

119122
it 'corrects anonymous jobs' do
120-
source = 'Class.new(ActiveJob::Base) {}'
121-
inspect_source(source)
122-
expect(cop.messages).to eq(['Jobs should subclass `ApplicationJob`.'])
123-
expect(cop.highlights).to eq(['ActiveJob::Base'])
124-
expect(autocorrect_source(source))
125-
.to eq('Class.new(ApplicationJob) {}')
123+
expect_offense(<<-RUBY.strip_indent)
124+
Class.new(ActiveJob::Base) {}
125+
^^^^^^^^^^^^^^^ Jobs should subclass `ApplicationJob`.
126+
RUBY
127+
128+
expect_correction(<<-RUBY.strip_indent)
129+
Class.new(ApplicationJob) {}
130+
RUBY
126131
end
127132

128133
it 'allows ApplicationJob defined using Class.new' do

spec/rubocop/cop/rails/application_record_spec.rb

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Rails::ApplicationRecord do
4-
let(:msgs) { ['Models should subclass `ApplicationRecord`.'] }
5-
64
context 'rails 4', :rails4, :config do
75
subject(:cop) { described_class.new(config) }
86

@@ -64,66 +62,90 @@ class ApplicationRecord < ActiveRecord::Base
6462
end
6563

6664
it 'corrects models that subclass ActiveRecord::Base' do
67-
source = "class MyModel < ActiveRecord::Base\nend"
68-
inspect_source(source)
69-
expect(cop.messages).to eq(msgs)
70-
expect(cop.highlights).to eq(['ActiveRecord::Base'])
71-
expect(autocorrect_source(source))
72-
.to eq("class MyModel < ApplicationRecord\nend")
65+
expect_offense(<<-RUBY.strip_indent)
66+
class MyModel < ActiveRecord::Base
67+
^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`.
68+
end
69+
RUBY
70+
71+
expect_correction(<<-RUBY.strip_indent)
72+
class MyModel < ApplicationRecord
73+
end
74+
RUBY
7375
end
7476

7577
it 'corrects single-line class definitions' do
76-
source = 'class MyModel < ActiveRecord::Base; end'
77-
inspect_source(source)
78-
expect(cop.messages).to eq(msgs)
79-
expect(cop.highlights).to eq(['ActiveRecord::Base'])
80-
expect(autocorrect_source(source))
81-
.to eq('class MyModel < ApplicationRecord; end')
78+
expect_offense(<<-RUBY.strip_indent)
79+
class MyModel < ActiveRecord::Base; end
80+
^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`.
81+
RUBY
82+
83+
expect_correction(<<-RUBY.strip_indent)
84+
class MyModel < ApplicationRecord; end
85+
RUBY
8286
end
8387

8488
it 'corrects namespaced models that subclass ActiveRecord::Base' do
85-
source = "module Nested\n class MyModel < ActiveRecord::Base\n end\nend"
86-
inspect_source(source)
87-
expect(cop.messages).to eq(msgs)
88-
expect(cop.highlights).to eq(['ActiveRecord::Base'])
89-
expect(autocorrect_source(source))
90-
.to eq("module Nested\n class MyModel < ApplicationRecord\n end\nend")
89+
expect_offense(<<-RUBY.strip_indent)
90+
module Nested
91+
class MyModel < ActiveRecord::Base
92+
^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`.
93+
end
94+
end
95+
RUBY
96+
97+
expect_correction(<<-RUBY.strip_indent)
98+
module Nested
99+
class MyModel < ApplicationRecord
100+
end
101+
end
102+
RUBY
91103
end
92104

93105
it 'corrects models defined using nested constants' do
94-
source = "class Nested::MyModel < ActiveRecord::Base\nend"
95-
inspect_source(source)
96-
expect(cop.messages).to eq(msgs)
97-
expect(cop.highlights).to eq(['ActiveRecord::Base'])
98-
expect(autocorrect_source(source))
99-
.to eq("class Nested::MyModel < ApplicationRecord\nend")
106+
expect_offense(<<-RUBY.strip_indent)
107+
class Nested::MyModel < ActiveRecord::Base
108+
^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`.
109+
end
110+
RUBY
111+
112+
expect_correction(<<-RUBY.strip_indent)
113+
class Nested::MyModel < ApplicationRecord
114+
end
115+
RUBY
100116
end
101117

102118
it 'corrects models defined using Class.new' do
103-
source = 'MyModel = Class.new(ActiveRecord::Base)'
104-
inspect_source(source)
105-
expect(cop.messages).to eq(msgs)
106-
expect(cop.highlights).to eq(['ActiveRecord::Base'])
107-
expect(autocorrect_source(source))
108-
.to eq('MyModel = Class.new(ApplicationRecord)')
119+
expect_offense(<<-RUBY.strip_indent)
120+
MyModel = Class.new(ActiveRecord::Base)
121+
^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`.
122+
RUBY
123+
124+
expect_correction(<<-RUBY.strip_indent)
125+
MyModel = Class.new(ApplicationRecord)
126+
RUBY
109127
end
110128

111129
it 'corrects nested models defined using Class.new' do
112-
source = 'Nested::MyModel = Class.new(ActiveRecord::Base)'
113-
inspect_source(source)
114-
expect(cop.messages).to eq(msgs)
115-
expect(cop.highlights).to eq(['ActiveRecord::Base'])
116-
expect(autocorrect_source(source))
117-
.to eq('Nested::MyModel = Class.new(ApplicationRecord)')
130+
expect_offense(<<-RUBY.strip_indent)
131+
Nested::MyModel = Class.new(ActiveRecord::Base)
132+
^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`.
133+
RUBY
134+
135+
expect_correction(<<-RUBY.strip_indent)
136+
Nested::MyModel = Class.new(ApplicationRecord)
137+
RUBY
118138
end
119139

120140
it 'corrects anonymous models' do
121-
source = 'Class.new(ActiveRecord::Base) {}'
122-
inspect_source(source)
123-
expect(cop.messages).to eq(msgs)
124-
expect(cop.highlights).to eq(['ActiveRecord::Base'])
125-
expect(autocorrect_source(source))
126-
.to eq('Class.new(ApplicationRecord) {}')
141+
expect_offense(<<-RUBY.strip_indent)
142+
Class.new(ActiveRecord::Base) {}
143+
^^^^^^^^^^^^^^^^^^ Models should subclass `ApplicationRecord`.
144+
RUBY
145+
146+
expect_correction(<<-RUBY.strip_indent)
147+
Class.new(ApplicationRecord) {}
148+
RUBY
127149
end
128150

129151
it 'allows ApplicationRecord defined using Class.new' do

0 commit comments

Comments
 (0)