|
67 | 67 | RUBY
|
68 | 68 | end
|
69 | 69 |
|
70 |
| - it 'flags change matcher when receiver is a variable' do |
| 70 | + it 'flags change matcher when receiver is a constant' do |
71 | 71 | expect_offense(<<-RUBY)
|
72 | 72 | it do
|
73 | 73 | expect { run }.to change(User, :count)
|
|
82 | 82 | RUBY
|
83 | 83 | end
|
84 | 84 |
|
| 85 | + it 'flags change matcher when receiver is a top-level constant' do |
| 86 | + expect_offense(<<-RUBY) |
| 87 | + it do |
| 88 | + expect { run }.to change(::User, :count) |
| 89 | + ^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { ::User.count }`. |
| 90 | + end |
| 91 | + RUBY |
| 92 | + |
| 93 | + expect_correction(<<-RUBY) |
| 94 | + it do |
| 95 | + expect { run }.to change { ::User.count } |
| 96 | + end |
| 97 | + RUBY |
| 98 | + end |
| 99 | + |
| 100 | + it 'flags change matcher when receiver is a variable' do |
| 101 | + expect_offense(<<-RUBY) |
| 102 | + it do |
| 103 | + expect { run }.to change(user, :status) |
| 104 | + ^^^^^^^^^^^^^^^^^^^^^ Prefer `change { user.status }`. |
| 105 | + end |
| 106 | + RUBY |
| 107 | + |
| 108 | + expect_correction(<<-RUBY) |
| 109 | + it do |
| 110 | + expect { run }.to change { user.status } |
| 111 | + end |
| 112 | + RUBY |
| 113 | + end |
| 114 | + |
| 115 | + it 'registers an offense for change matcher with chained method call' do |
| 116 | + expect_offense(<<-RUBY) |
| 117 | + it do |
| 118 | + expect { paint_users! }.to change(users.green, :count).by(1) |
| 119 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { users.green.count }`. |
| 120 | + end |
| 121 | + RUBY |
| 122 | + |
| 123 | + expect_correction(<<-RUBY) |
| 124 | + it do |
| 125 | + expect { paint_users! }.to change { users.green.count }.by(1) |
| 126 | + end |
| 127 | + RUBY |
| 128 | + end |
| 129 | + |
| 130 | + it 'registers an offense for change matcher with an instance variable' do |
| 131 | + expect_offense(<<-RUBY) |
| 132 | + it do |
| 133 | + expect { paint_users! }.to change(@food, :taste).to(:sour) |
| 134 | + ^^^^^^^^^^^^^^^^^^^^^ Prefer `change { @food.taste }`. |
| 135 | + end |
| 136 | + RUBY |
| 137 | + |
| 138 | + expect_correction(<<-RUBY) |
| 139 | + it do |
| 140 | + expect { paint_users! }.to change { @food.taste }.to(:sour) |
| 141 | + end |
| 142 | + RUBY |
| 143 | + end |
| 144 | + |
| 145 | + it 'registers an offense for change matcher with a global variable' do |
| 146 | + expect_offense(<<-RUBY) |
| 147 | + it do |
| 148 | + expect { paint_users! }.to change($token, :value).to(nil) |
| 149 | + ^^^^^^^^^^^^^^^^^^^^^^ Prefer `change { $token.value }`. |
| 150 | + end |
| 151 | + RUBY |
| 152 | + |
| 153 | + expect_correction(<<-RUBY) |
| 154 | + it do |
| 155 | + expect { paint_users! }.to change { $token.value }.to(nil) |
| 156 | + end |
| 157 | + RUBY |
| 158 | + end |
| 159 | + |
85 | 160 | it 'ignores methods called change' do
|
86 | 161 | expect_no_offenses(<<-RUBY)
|
87 | 162 | it do
|
|
0 commit comments