From 17385f4dc42d86d7606e3359b9db82cb43d5e534 Mon Sep 17 00:00:00 2001 From: Vitali Tatarintev Date: Mon, 26 Aug 2019 15:13:41 +0200 Subject: [PATCH] Refactor BeSuccessMatcher specs for readability --- .rubocop.yml | 1 + rubocop/cop/rspec/be_success_matcher.rb | 2 +- .../cop/rspec/be_success_matcher_spec.rb | 65 +++++++++---------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 1319752fc9c..573f2fbb6c6 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -273,3 +273,4 @@ RSpec/BeSuccessMatcher: - 'spec/support/shared_examples/controllers/**/*' - 'ee/spec/support/shared_examples/controllers/**/*' - 'spec/support/controllers/**/*' + - 'ee/spec/support/controllers/**/*' diff --git a/rubocop/cop/rspec/be_success_matcher.rb b/rubocop/cop/rspec/be_success_matcher.rb index 0b7d737f4e2..07dad061613 100644 --- a/rubocop/cop/rspec/be_success_matcher.rb +++ b/rubocop/cop/rspec/be_success_matcher.rb @@ -26,7 +26,7 @@ module RuboCop class BeSuccessMatcher < RuboCop::Cop::Cop include SpecHelpers - MESSAGE = 'Do not use deprecated `success?` method, use `successful?` instead.'.freeze + MESSAGE = 'Do not use deprecated `success?` method, use `successful?` instead.' def_node_search :expect_to_be_success?, <<~PATTERN (send (send nil? :expect (send nil? ...)) {:to :not_to :to_not} (send nil? :be_success)) diff --git a/spec/rubocop/cop/rspec/be_success_matcher_spec.rb b/spec/rubocop/cop/rspec/be_success_matcher_spec.rb index 77aff7c9dcc..0b4e9853dae 100644 --- a/spec/rubocop/cop/rspec/be_success_matcher_spec.rb +++ b/spec/rubocop/cop/rspec/be_success_matcher_spec.rb @@ -10,33 +10,6 @@ require_relative '../../../../rubocop/cop/rspec/be_success_matcher' describe RuboCop::Cop::RSpec::BeSuccessMatcher do include CopHelper - CODE_EXAMPLES = [ - { - bad: %(expect(response).to be_success).freeze, - good: %(expect(response).to be_successful).freeze - }, - { - bad: %(expect(response).to_not be_success).freeze, - good: %(expect(response).to_not be_successful).freeze - }, - { - bad: %(expect(response).not_to be_success).freeze, - good: %(expect(response).not_to be_successful).freeze - }, - { - bad: %(is_expected.to be_success).freeze, - good: %(is_expected.to be_successful).freeze - }, - { - bad: %(is_expected.to_not be_success).freeze, - good: %(is_expected.to_not be_successful).freeze - }, - { - bad: %(is_expected.not_to be_success).freeze, - good: %(is_expected.not_to be_successful).freeze - } - ].freeze - let(:source_file) { 'spec/foo_spec.rb' } subject(:cop) { described_class.new } @@ -59,18 +32,44 @@ describe RuboCop::Cop::RSpec::BeSuccessMatcher do end end - CODE_EXAMPLES.each do |code_example| - context "using #{code_example[:bad]} call" do - it_behaves_like 'an offensive be_success call', code_example[:bad] - it_behaves_like 'an autocorrected be_success call', code_example[:bad], code_example[:good] + shared_examples 'cop' do |good:, bad:| + context "using #{bad} call" do + it_behaves_like 'an offensive be_success call', bad + it_behaves_like 'an autocorrected be_success call', bad, good end - context "using #{code_example[:good]} call" do + context "using #{good} call" do it 'does not register an offense' do - inspect_source(code_example[:good]) + inspect_source(good) expect(cop.offenses.size).to eq(0) end end end + + describe 'using different code examples' do + it_behaves_like 'cop', + bad: 'expect(response).to be_success', + good: 'expect(response).to be_successful' + + it_behaves_like 'cop', + bad: 'expect(response).to_not be_success', + good: 'expect(response).to_not be_successful' + + it_behaves_like 'cop', + bad: 'expect(response).not_to be_success', + good: 'expect(response).not_to be_successful' + + it_behaves_like 'cop', + bad: 'is_expected.to be_success', + good: 'is_expected.to be_successful' + + it_behaves_like 'cop', + bad: 'is_expected.to_not be_success', + good: 'is_expected.to_not be_successful' + + it_behaves_like 'cop', + bad: 'is_expected.not_to be_success', + good: 'is_expected.not_to be_successful' + end end