|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::ChangeWithoutExpect, :config do |
| 4 | + it 'registers an offense for a `change` call with only `by` ' \ |
| 5 | + 'without an `expect` block' do |
| 6 | + expect_offense(<<~RUBY) |
| 7 | + it 'changes the count' do |
| 8 | + change(Counter, :count).by(1) |
| 9 | + ^^^^^^^^^^^^^^^^^^^^^^^ Use `change` matcher within an `expect` block. |
| 10 | + end |
| 11 | + RUBY |
| 12 | + end |
| 13 | + |
| 14 | + it 'registers an offense for a `change` call with only `by_at_least` ' \ |
| 15 | + 'without an `expect` block' do |
| 16 | + expect_offense(<<~RUBY) |
| 17 | + it 'changes the count' do |
| 18 | + change(Counter, :count).by_at_least(1) |
| 19 | + ^^^^^^^^^^^^^^^^^^^^^^^ Use `change` matcher within an `expect` block. |
| 20 | + end |
| 21 | + RUBY |
| 22 | + end |
| 23 | + |
| 24 | + it 'registers an offense for a `change` call with only `by_at_most` ' \ |
| 25 | + 'without an `expect` block' do |
| 26 | + expect_offense(<<~RUBY) |
| 27 | + it 'changes the count' do |
| 28 | + change(Counter, :count).by_at_most(1) |
| 29 | + ^^^^^^^^^^^^^^^^^^^^^^^ Use `change` matcher within an `expect` block. |
| 30 | + end |
| 31 | + RUBY |
| 32 | + end |
| 33 | + |
| 34 | + it 'registers an offense for a `change` call with only `from` ' \ |
| 35 | + 'without an `expect` block' do |
| 36 | + expect_offense(<<~RUBY) |
| 37 | + it 'changes the count' do |
| 38 | + change(Counter, :count).from(0) |
| 39 | + ^^^^^^^^^^^^^^^^^^^^^^^ Use `change` matcher within an `expect` block. |
| 40 | + end |
| 41 | + RUBY |
| 42 | + end |
| 43 | + |
| 44 | + it 'registers an offense for a `change` call with exactly `from().to()` ' \ |
| 45 | + 'without an `expect` block' do |
| 46 | + expect_offense(<<~RUBY) |
| 47 | + it 'changes the count' do |
| 48 | + change(Counter, :count).from(0).to(1) |
| 49 | + ^^^^^^^^^^^^^^^^^^^^^^^ Use `change` matcher within an `expect` block. |
| 50 | + end |
| 51 | + RUBY |
| 52 | + end |
| 53 | + |
| 54 | + it 'does not register an offense for a `change` call with `from().by()` ' \ |
| 55 | + 'without an `expect` block' do |
| 56 | + expect_no_offenses(<<~RUBY) |
| 57 | + it 'changes the count' do |
| 58 | + change(Counter, :count).from(0).by(1) |
| 59 | + end |
| 60 | + RUBY |
| 61 | + end |
| 62 | + |
| 63 | + it 'does not register an offense for a simple `change` call ' \ |
| 64 | + 'without chains' do |
| 65 | + expect_no_offenses(<<~RUBY) |
| 66 | + it 'changes the count' do |
| 67 | + change(Counter, :count) |
| 68 | + end |
| 69 | + RUBY |
| 70 | + end |
| 71 | + |
| 72 | + it 'does not register an offense for a `change` call with unsupported ' \ |
| 73 | + 'chain method' do |
| 74 | + expect_no_offenses(<<~RUBY) |
| 75 | + it 'changes the count' do |
| 76 | + change(Counter, :count).some_other_method(123) |
| 77 | + end |
| 78 | + RUBY |
| 79 | + end |
| 80 | + |
| 81 | + it 'does not register an offense for `change` with chains within an ' \ |
| 82 | + '`expect` block' do |
| 83 | + expect_no_offenses(<<~RUBY) |
| 84 | + it 'changes the count' do |
| 85 | + expect { subject }.to change(Counter, :count).by(1) |
| 86 | + end |
| 87 | + RUBY |
| 88 | + end |
| 89 | + |
| 90 | + it 'does not register an offense for `change` with from-to chain ' \ |
| 91 | + 'within an `expect` block' do |
| 92 | + expect_no_offenses(<<~RUBY) |
| 93 | + it 'changes the count' do |
| 94 | + expect { subject }.to change(Counter, :count).from(0).to(1) |
| 95 | + end |
| 96 | + RUBY |
| 97 | + end |
| 98 | + |
| 99 | + it 'does not register an offense for `change` with chains ' \ |
| 100 | + 'within an `expect` with not_to' do |
| 101 | + expect_no_offenses(<<~RUBY) |
| 102 | + it 'does not change the count' do |
| 103 | + expect { subject }.not_to change(Counter, :count).by(1) |
| 104 | + end |
| 105 | + RUBY |
| 106 | + end |
| 107 | +end |
0 commit comments