|
23 | 23 | RUBY
|
24 | 24 | end
|
25 | 25 |
|
26 |
| - it 'ignores blocks that cannot be converted to obj/attribute pair' do |
| 26 | + it 'ignores blocks when the method is called with arguments' do |
27 | 27 | expect_no_offenses(<<-RUBY)
|
28 | 28 | it do
|
29 | 29 | expect { run }.to change { User.sum(:points) }
|
30 | 30 | end
|
31 | 31 | RUBY
|
32 | 32 | end
|
33 | 33 |
|
34 |
| - it 'ignores change method of object that happens to receive a block' do |
| 34 | + it 'ignores when not an expectation' do |
35 | 35 | expect_no_offenses(<<-RUBY)
|
36 | 36 | it do
|
37 | 37 | Record.change { User.count }
|
|
47 | 47 | end
|
48 | 48 | RUBY
|
49 | 49 | end
|
| 50 | + |
| 51 | + it 'ignores the usage that adheres to the enforced style' do |
| 52 | + expect_no_offenses(<<-RUBY) |
| 53 | + it do |
| 54 | + expect { run }.to change(User, :count).by(1) |
| 55 | + end |
| 56 | + RUBY |
| 57 | + end |
| 58 | + |
| 59 | + it 'flags when the received is a namespaced constant' do |
| 60 | + expect_offense(<<-RUBY) |
| 61 | + it do |
| 62 | + expect { run }.to change { User::Token::Auth.count }.by(1) |
| 63 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change(User::Token::Auth, :count)`. |
| 64 | + end |
| 65 | + RUBY |
| 66 | + |
| 67 | + expect_correction(<<-RUBY) |
| 68 | + it do |
| 69 | + expect { run }.to change(User::Token::Auth, :count).by(1) |
| 70 | + end |
| 71 | + RUBY |
| 72 | + end |
| 73 | + |
| 74 | + it 'flags when the receiver is a top-level constant' do |
| 75 | + expect_offense(<<-RUBY) |
| 76 | + it do |
| 77 | + expect { run }.to change { ::User.count }.by(1) |
| 78 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change(::User, :count)`. |
| 79 | + end |
| 80 | + RUBY |
| 81 | + |
| 82 | + expect_correction(<<-RUBY) |
| 83 | + it do |
| 84 | + expect { run }.to change(::User, :count).by(1) |
| 85 | + end |
| 86 | + RUBY |
| 87 | + end |
| 88 | + |
| 89 | + it 'flags chained method calls' do |
| 90 | + expect_offense(<<-RUBY) |
| 91 | + it do |
| 92 | + expect { file.upload! }.to change { user.uploads.count }.by(1) |
| 93 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change(user.uploads, :count)`. |
| 94 | + end |
| 95 | + RUBY |
| 96 | + end |
50 | 97 | end
|
51 | 98 |
|
52 | 99 | context 'with EnforcedStyle `block`' do
|
|
67 | 114 | RUBY
|
68 | 115 | end
|
69 | 116 |
|
70 |
| - it 'flags change matcher when receiver is a constant' do |
| 117 | + it 'flags change matcher when receiver is a namespaced constant' do |
71 | 118 | expect_offense(<<-RUBY)
|
72 | 119 | it do
|
73 |
| - expect { run }.to change(User, :count) |
74 |
| - ^^^^^^^^^^^^^^^^^^^^ Prefer `change { User.count }`. |
| 120 | + expect { run }.to change(User::Token::Auth, :count).by(1) |
| 121 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { User::Token::Auth.count }`. |
75 | 122 | end
|
76 | 123 | RUBY
|
77 | 124 |
|
78 | 125 | expect_correction(<<-RUBY)
|
79 | 126 | it do
|
80 |
| - expect { run }.to change { User.count } |
| 127 | + expect { run }.to change { User::Token::Auth.count }.by(1) |
81 | 128 | end
|
82 | 129 | RUBY
|
83 | 130 | end
|
84 | 131 |
|
85 |
| - it 'flags change matcher when receiver is a top-level constant' do |
| 132 | + it 'flags change matcher when receiver is a variable' do |
86 | 133 | expect_offense(<<-RUBY)
|
87 | 134 | it do
|
88 |
| - expect { run }.to change(::User, :count) |
89 |
| - ^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { ::User.count }`. |
| 135 | + expect { run }.to change(user, :count) |
| 136 | + ^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.count }`. |
90 | 137 | end
|
91 | 138 | RUBY
|
92 | 139 |
|
93 | 140 | expect_correction(<<-RUBY)
|
94 | 141 | it do
|
95 |
| - expect { run }.to change { ::User.count } |
| 142 | + expect { run }.to change { user.count } |
96 | 143 | end
|
97 | 144 | RUBY
|
98 | 145 | end
|
99 | 146 |
|
100 |
| - it 'flags change matcher when receiver is a variable' do |
| 147 | + it 'flags change matcher when message is a string' do |
101 | 148 | expect_offense(<<-RUBY)
|
102 | 149 | it do
|
103 |
| - expect { run }.to change(user, :status) |
104 |
| - ^^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.status }`. |
| 150 | + expect { run }.to change(user, 'status') |
| 151 | + ^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.status }`. |
105 | 152 | end
|
106 | 153 | RUBY
|
107 | 154 |
|
|
127 | 174 | RUBY
|
128 | 175 | end
|
129 | 176 |
|
| 177 | + it 'flags change matcher when receiver is a top-level constant' do |
| 178 | + expect_offense(<<-RUBY) |
| 179 | + it do |
| 180 | + expect { run }.to change(::User, :count) |
| 181 | + ^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { ::User.count }`. |
| 182 | + end |
| 183 | + RUBY |
| 184 | + |
| 185 | + expect_correction(<<-RUBY) |
| 186 | + it do |
| 187 | + expect { run }.to change { ::User.count } |
| 188 | + end |
| 189 | + RUBY |
| 190 | + end |
| 191 | + |
130 | 192 | it 'registers an offense for change matcher with an instance variable' do
|
131 | 193 | expect_offense(<<-RUBY)
|
132 | 194 | it do
|
|
0 commit comments