/
expect_change_spec.rb
102 lines (88 loc) · 2.54 KB
/
expect_change_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::RSpec::ExpectChange do
let(:cop_config) do
{ 'EnforcedStyle' => enforced_style }
end
context 'with EnforcedStyle `method_call`' do
let(:enforced_style) { 'method_call' }
it 'flags blocks that contain simple message sending' do
expect_offense(<<-RUBY)
it do
expect { run }.to change { User.count }.by(1)
^^^^^^^^^^^^^^^^^^^^^ Prefer `change(User, :count)`.
end
RUBY
expect_correction(<<-RUBY)
it do
expect { run }.to change(User, :count).by(1)
end
RUBY
end
it 'ignores blocks that cannot be converted to obj/attribute pair' do
expect_no_offenses(<<-RUBY)
it do
expect { run }.to change { User.sum(:points) }
end
RUBY
end
it 'ignores change method of object that happens to receive a block' do
expect_no_offenses(<<-RUBY)
it do
Record.change { User.count }
end
RUBY
end
it 'flags implicit block expectation syntax' do
expect_offense(<<-RUBY)
it do
expect(run).to change { User.count }.by(1)
^^^^^^^^^^^^^^^^^^^^^ Prefer `change(User, :count)`.
end
RUBY
end
end
context 'with EnforcedStyle `block`' do
let(:enforced_style) { 'block' }
it 'flags change matcher without block' do
expect_offense(<<-RUBY)
it do
expect { run }.to change(User, :count).by(1)
^^^^^^^^^^^^^^^^^^^^ Prefer `change { User.count }`.
end
RUBY
expect_correction(<<-RUBY)
it do
expect { run }.to change { User.count }.by(1)
end
RUBY
end
it 'flags change matcher when receiver is a variable' do
expect_offense(<<-RUBY)
it do
expect { run }.to change(User, :count)
^^^^^^^^^^^^^^^^^^^^ Prefer `change { User.count }`.
end
RUBY
expect_correction(<<-RUBY)
it do
expect { run }.to change { User.count }
end
RUBY
end
it 'ignores methods called change' do
expect_no_offenses(<<-RUBY)
it do
record.change(user, :count)
end
RUBY
end
it 'flags implicit block expectation syntax' do
expect_offense(<<-RUBY)
it do
expect(run).to change(User, :count).by(1)
^^^^^^^^^^^^^^^^^^^^ Prefer `change { User.count }`.
end
RUBY
end
end
end