|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::ChangeByZero do |
| 4 | + it 'registers an offense when the argument to `by` is zero' do |
| 5 | + expect_offense(<<-RUBY) |
| 6 | + it do |
| 7 | + expect { foo }.to change(Foo, :bar).by(0) |
| 8 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_to change` over `to change.by(0)`. |
| 9 | + expect { foo }.to change(::Foo, :bar).by(0) |
| 10 | + ^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_to change` over `to change.by(0)`. |
| 11 | + expect { foo }.to change { Foo.bar }.by(0) |
| 12 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_to change` over `to change.by(0)`. |
| 13 | + expect { foo }.to change(Foo, :bar).by 0 |
| 14 | + ^^^^^^^^^^^^^^^^^^^^^^ Prefer `not_to change` over `to change.by(0)`. |
| 15 | + end |
| 16 | + RUBY |
| 17 | + |
| 18 | + expect_correction(<<-RUBY) |
| 19 | + it do |
| 20 | + expect { foo }.not_to change(Foo, :bar) |
| 21 | + expect { foo }.not_to change(::Foo, :bar) |
| 22 | + expect { foo }.not_to change { Foo.bar } |
| 23 | + expect { foo }.not_to change(Foo, :bar) |
| 24 | + end |
| 25 | + RUBY |
| 26 | + end |
| 27 | + |
| 28 | + it 'registers an offense when the argument to `by` is zero ' \ |
| 29 | + 'with compound expectations' do |
| 30 | + expect_offense(<<-RUBY) |
| 31 | + it do |
| 32 | + expect { foo } |
| 33 | + .to change(Foo, :bar).by(0) |
| 34 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 35 | + .and change(Foo, :baz).by(0) |
| 36 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 37 | + expect { foo } |
| 38 | + .to change { Foo.bar }.by(0) |
| 39 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 40 | + .and change { Foo.baz }.by(0) |
| 41 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 42 | + expect { foo } |
| 43 | + .to change(Foo, :bar).by(0) |
| 44 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 45 | + .or change(Foo, :baz).by(0) |
| 46 | + ^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 47 | + expect { foo } |
| 48 | + .to change { Foo.bar }.by(0) |
| 49 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 50 | + .or change { Foo.baz }.by(0) |
| 51 | + ^^^^^^^^^^^^^^^^^^^^^^^^ Prefer negated matchers with compound expectations over `change.by(0)`. |
| 52 | + end |
| 53 | + RUBY |
| 54 | + end |
| 55 | + |
| 56 | + it 'does not register an offense when the argument to `by` is not zero' do |
| 57 | + expect_no_offenses(<<-RUBY) |
| 58 | + it do |
| 59 | + expect { foo }.to change(Foo, :bar).by(1) |
| 60 | + expect { foo }.to change { Foo.bar }.by(1) |
| 61 | + end |
| 62 | + RUBY |
| 63 | + end |
| 64 | +end |
0 commit comments