|  | 
|  | 1 | +# frozen_string_literal: true | 
|  | 2 | + | 
|  | 3 | +RSpec.describe RuboCop::Cop::RSpec::ImplicitBlockExpectation do | 
|  | 4 | +  subject(:cop) { described_class.new } | 
|  | 5 | + | 
|  | 6 | +  it 'flags lambda in subject' do | 
|  | 7 | +    expect_offense(<<-RUBY) | 
|  | 8 | +      describe do | 
|  | 9 | +        subject { -> { boom } } | 
|  | 10 | +        it { is_expected.to change { something }.to(new_value) } | 
|  | 11 | +             ^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 12 | +      end | 
|  | 13 | +    RUBY | 
|  | 14 | +  end | 
|  | 15 | + | 
|  | 16 | +  it 'ignores non-lambda subject' do | 
|  | 17 | +    expect_no_offenses(<<-RUBY) | 
|  | 18 | +      describe do | 
|  | 19 | +        subject { 'normal' } | 
|  | 20 | +        it { is_expected.to eq(something) } | 
|  | 21 | +      end | 
|  | 22 | +    RUBY | 
|  | 23 | +  end | 
|  | 24 | + | 
|  | 25 | +  it 'flags lambda in subject!' do | 
|  | 26 | +    expect_offense(<<-RUBY) | 
|  | 27 | +      describe do | 
|  | 28 | +        subject! { -> { boom } } | 
|  | 29 | +        it { is_expected.to change { something }.to(new_value) } | 
|  | 30 | +             ^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 31 | +      end | 
|  | 32 | +    RUBY | 
|  | 33 | +  end | 
|  | 34 | + | 
|  | 35 | +  it 'flags literal lambda' do | 
|  | 36 | +    expect_offense(<<-RUBY) | 
|  | 37 | +      describe do | 
|  | 38 | +        subject! { lambda { boom } } | 
|  | 39 | +        it { is_expected.to change { something }.to(new_value) } | 
|  | 40 | +             ^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 41 | +      end | 
|  | 42 | +    RUBY | 
|  | 43 | +  end | 
|  | 44 | + | 
|  | 45 | +  it 'flags proc' do | 
|  | 46 | +    expect_offense(<<-RUBY) | 
|  | 47 | +      describe do | 
|  | 48 | +        subject! { proc { boom } } | 
|  | 49 | +        it { is_expected.to change { something }.to(new_value) } | 
|  | 50 | +             ^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 51 | +      end | 
|  | 52 | +    RUBY | 
|  | 53 | +  end | 
|  | 54 | + | 
|  | 55 | +  it 'flags Proc.new' do | 
|  | 56 | +    expect_offense(<<-RUBY) | 
|  | 57 | +      describe do | 
|  | 58 | +        subject! { Proc.new { boom } } | 
|  | 59 | +        it { is_expected.to change { something }.to(new_value) } | 
|  | 60 | +             ^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 61 | +      end | 
|  | 62 | +    RUBY | 
|  | 63 | +  end | 
|  | 64 | + | 
|  | 65 | +  it 'flags named subject' do | 
|  | 66 | +    expect_offense(<<-RUBY) | 
|  | 67 | +      describe do | 
|  | 68 | +        subject(:name) { -> { boom } } | 
|  | 69 | +        it { is_expected.to change { something }.to(new_value) } | 
|  | 70 | +             ^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 71 | +      end | 
|  | 72 | +    RUBY | 
|  | 73 | +  end | 
|  | 74 | + | 
|  | 75 | +  it 'flags when subject is defined in the outer example group' do | 
|  | 76 | +    expect_offense(<<-RUBY) | 
|  | 77 | +      describe do | 
|  | 78 | +        subject { -> { boom } } | 
|  | 79 | +        context do | 
|  | 80 | +          it { is_expected.to change { something }.to(new_value) } | 
|  | 81 | +               ^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 82 | +        end | 
|  | 83 | +      end | 
|  | 84 | +    RUBY | 
|  | 85 | +  end | 
|  | 86 | + | 
|  | 87 | +  it 'ignores normal local subject' do | 
|  | 88 | +    expect_no_offenses(<<-RUBY) | 
|  | 89 | +      describe do | 
|  | 90 | +        subject { -> { boom } } | 
|  | 91 | +        context do | 
|  | 92 | +          subject { 'normal' } | 
|  | 93 | +          it { is_expected.to eq(something) } | 
|  | 94 | +        end | 
|  | 95 | +      end | 
|  | 96 | +    RUBY | 
|  | 97 | +  end | 
|  | 98 | + | 
|  | 99 | +  it 'ignores named subject with deeply nested lambda' do | 
|  | 100 | +    expect_no_offenses(<<-RUBY) | 
|  | 101 | +      describe do | 
|  | 102 | +        subject { {hash: -> { boom }} } | 
|  | 103 | +        it { is_expected.to be(something) } | 
|  | 104 | +      end | 
|  | 105 | +    RUBY | 
|  | 106 | +  end | 
|  | 107 | + | 
|  | 108 | +  it 'flags with `should` as implicit subject' do | 
|  | 109 | +    expect_offense(<<-RUBY) | 
|  | 110 | +      describe do | 
|  | 111 | +        subject { -> { boom } } | 
|  | 112 | +        it { should change { something }.to(new_value) } | 
|  | 113 | +             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 114 | +      end | 
|  | 115 | +    RUBY | 
|  | 116 | +  end | 
|  | 117 | + | 
|  | 118 | +  it 'flags with `should_not` as implicit subject' do | 
|  | 119 | +    expect_offense(<<-RUBY) | 
|  | 120 | +      describe do | 
|  | 121 | +        subject { -> { boom } } | 
|  | 122 | +        it { should_not change { something }.to(new_value) } | 
|  | 123 | +             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid implicit block expectations. | 
|  | 124 | +      end | 
|  | 125 | +    RUBY | 
|  | 126 | +  end | 
|  | 127 | + | 
|  | 128 | +  it 'ignores when there is no subject defined' do | 
|  | 129 | +    expect_no_offenses(<<-RUBY) | 
|  | 130 | +      shared_examples 'subject is defined somewhere else' do | 
|  | 131 | +        it { is_expected.to change { something }.to(new_value) } | 
|  | 132 | +      end | 
|  | 133 | +    RUBY | 
|  | 134 | +  end | 
|  | 135 | +end | 
0 commit comments