spec/rubocop/cop/rspec/implicit_subject_spec.rb in rubocop-rspec-1.32.0 vs spec/rubocop/cop/rspec/implicit_subject_spec.rb in rubocop-rspec-1.33.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + RSpec.describe RuboCop::Cop::RSpec::ImplicitSubject, :config do subject(:cop) { described_class.new(config) } let(:cop_config) do { 'EnforcedStyle' => enforced_style } @@ -13,10 +15,16 @@ it 'expect subject to be used' do is_expected.to be_good ^^^^^^^^^^^ Don't use implicit subject. end RUBY + + expect_correction(<<-RUBY) + it 'expect subject to be used' do + expect(subject).to be_good + end + RUBY end it 'allows `is_expected` inside `its` block, in multi-line examples' do expect_no_offenses(<<-RUBY) its(:quality) do @@ -28,12 +36,21 @@ it 'flags `should` in multi-line examples' do expect_offense(<<-RUBY) it 'expect subject to be used' do should be_good ^^^^^^^^^^^^^^ Don't use implicit subject. + should_not be_bad + ^^^^^^^^^^^^^^^^^ Don't use implicit subject. end RUBY + + expect_correction(<<-RUBY) + it 'expect subject to be used' do + expect(subject).to be_good + expect(subject).not_to be_bad + end + RUBY end it 'allows `is_expected` in single-line examples' do expect_no_offenses(<<-RUBY) it { is_expected.to be_good } @@ -61,45 +78,17 @@ def permits(actions) actions.each { |action| is_expected.to permit_action(action) } ^^^^^^^^^^^ Don't use implicit subject. end RUBY - end - bad_code = <<-RUBY - it 'works' do - is_expected.to be_truthy - end - RUBY - - good_code = <<-RUBY - it 'works' do - expect(subject).to be_truthy - end - RUBY - - include_examples 'autocorrect', - bad_code, - good_code - - bad_code = <<-RUBY - it 'works' do - should be_truthy - should_not be_falsy - end - RUBY - - good_code = <<-RUBY - it 'works' do - expect(subject).to be_truthy - expect(subject).not_to be_falsy - end - RUBY - - include_examples 'autocorrect', - bad_code, - good_code + expect_correction(<<-RUBY) + def permits(actions) + actions.each { |action| expect(subject).to permit_action(action) } + end + RUBY + end end context 'with EnforcedStyle `single_statement_only`' do let(:enforced_style) { 'single_statement_only' } @@ -117,33 +106,18 @@ subject.age = 18 is_expected.to be_valid ^^^^^^^^^^^ Don't use implicit subject. end RUBY - end - bad_code = <<-RUBY - it 'is valid' do - subject.age = 18 - is_expected.to be_valid - end - RUBY - - good_code = <<-RUBY - it 'is valid' do - subject.age = 18 - expect(subject).to be_valid - end - RUBY - - include_examples 'autocorrect', - bad_code, - good_code - - include_examples 'autocorrect', - bad_code, - good_code + expect_correction(<<-RUBY) + it 'expect subject to be used' do + subject.age = 18 + expect(subject).to be_valid + end + RUBY + end end context 'with EnforcedStyle `disallow`' do let(:enforced_style) { 'disallow' } @@ -152,49 +126,63 @@ it 'expect subject to be used' do is_expected.to be_good ^^^^^^^^^^^ Don't use implicit subject. end RUBY + + expect_correction(<<-RUBY) + it 'expect subject to be used' do + expect(subject).to be_good + end + RUBY end it 'flags `is_expected` in single-line examples' do expect_offense(<<-RUBY) it { is_expected.to be_good } ^^^^^^^^^^^ Don't use implicit subject. RUBY + + expect_correction(<<-RUBY) + it { expect(subject).to be_good } + RUBY end it 'flags `should` in multi-line examples' do expect_offense(<<-RUBY) it 'expect subject to be used' do should be_good ^^^^^^^^^^^^^^ Don't use implicit subject. + should_not be_bad + ^^^^^^^^^^^^^^^^^ Don't use implicit subject. end RUBY + + expect_correction(<<-RUBY) + it 'expect subject to be used' do + expect(subject).to be_good + expect(subject).not_to be_bad + end + RUBY end it 'flags `should` in single-line examples' do expect_offense(<<-RUBY) it { should be_good } ^^^^^^^^^^^^^^ Don't use implicit subject. + it { should_not be_bad } + ^^^^^^^^^^^^^^^^^ Don't use implicit subject. RUBY + + expect_correction(<<-RUBY) + it { expect(subject).to be_good } + it { expect(subject).not_to be_bad } + RUBY end it 'allows `is_expected` inside `its` block' do expect_no_offenses(<<-RUBY) its(:quality) { is_expected.to be :high } RUBY end - - include_examples 'autocorrect', - 'it { is_expected.to be_truthy }', - 'it { expect(subject).to be_truthy }' - - include_examples 'autocorrect', - 'it { should be_truthy }', - 'it { expect(subject).to be_truthy }' - - include_examples 'autocorrect', - 'it { should_not be_truthy }', - 'it { expect(subject).not_to be_truthy }' end end