From 1684470a0ee08ade63dbbfe416716a6fc5a47b61 Mon Sep 17 00:00:00 2001 From: Francisco Quintero Coronell Date: Tue, 1 Dec 2020 15:35:03 -0500 Subject: [PATCH] Add clearer examples for the SubjectStub Cop --- docs/modules/ROOT/pages/cops_rspec.adoc | 18 ++++++++++++++---- lib/rubocop/cop/rspec/subject_stub.rb | 18 ++++++++++++++---- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/docs/modules/ROOT/pages/cops_rspec.adoc b/docs/modules/ROOT/pages/cops_rspec.adoc index db84dfcb6..2a586db2d 100644 --- a/docs/modules/ROOT/pages/cops_rspec.adoc +++ b/docs/modules/ROOT/pages/cops_rspec.adoc @@ -3822,11 +3822,21 @@ Checks for stubbed test subjects. [source,ruby] ---- # bad -describe Foo do - subject(:bar) { baz } +describe Article do + subject(:article) { Article.new } - before do - allow(bar).to receive(:qux?).and_return(true) + it 'indicates that the author is unknown' do + allow(article).to receive(:author).and_return(nil) + expect(article.description).to include('by an unknown author') + end +end + +# good +describe Article do + subject(:article) { Article.new(author: nil) } + + it 'indicates that the author is unknown' do + expect(article.description).to include('by an unknown author') end end ---- diff --git a/lib/rubocop/cop/rspec/subject_stub.rb b/lib/rubocop/cop/rspec/subject_stub.rb index ca5d04d6a..5e55c028a 100644 --- a/lib/rubocop/cop/rspec/subject_stub.rb +++ b/lib/rubocop/cop/rspec/subject_stub.rb @@ -13,11 +13,21 @@ module RSpec # # @example # # bad - # describe Foo do - # subject(:bar) { baz } + # describe Article do + # subject(:article) { Article.new } # - # before do - # allow(bar).to receive(:qux?).and_return(true) + # it 'indicates that the author is unknown' do + # allow(article).to receive(:author).and_return(nil) + # expect(article.description).to include('by an unknown author') + # end + # end + # + # # good + # describe Article do + # subject(:article) { Article.new(author: nil) } + # + # it 'indicates that the author is unknown' do + # expect(article.description).to include('by an unknown author') # end # end #