class RuboCop::Cop::RSpec::SubjectStub
end
end
expect(article.description).to include(‘by an unknown author’)
it ‘indicates that the author is unknown’ do
subject(:article) { Article.new(author: nil) }
describe Article do
# good
end
end
end
expect(article.description).to include(‘by an unknown author’)
allow(article).to receive(:author).and_return(nil)
it ‘indicates that the author is unknown’ do
subject(:article) { Article.new }
context ‘nested subject’ do
subject(:foo) { Article.new }
describe Article do
# bad
end
end
expect(article.description).to include(‘by an unknown author’)
allow(article).to receive(:author).and_return(nil)
it ‘indicates that the author is unknown’ do
subject(:article) { Article.new }
describe Article do
# bad
@example
@see github.com/rubocop-hq/rspec-style-guide#dont-stub-subject<br>@see penelope.zone/2015/12/27/introducing-rspec-smells-and-where-to-find-them.html#smell-1-stubjec<br>@see robots.thoughtbot.com/don-t-stub-the-system-under-test<br><br>when subject is also defined in parent example groups.
Checks nested subject stubs for innermost subject definition
Checks for stubbed test subjects.
def find_all_explicit(node)
def find_all_explicit(node) node.each_descendant(:block).with_object({}) do |child, h| name = yield(child) next unless name outer_example_group = child.each_ancestor(:block).find do |a| example_group?(a) end h[outer_example_group] ||= [] h[outer_example_group] << name end end
def find_subject_expectations(node, subject_names = [], &block)
def find_subject_expectations(node, subject_names = [], &block) subject_names = [*subject_names, *@explicit_subjects[node]] subject_names -= @subject_overrides[node] if @subject_overrides[node] names = Set[*subject_names, :subject] expectation_detected = message_expectation?(node, names) return yield(node) if expectation_detected node.each_child_node(:send, :def, :block, :begin) do |child| find_subject_expectations(child, subject_names, &block) end end
def on_top_level_group(node)
def on_top_level_group(node) @explicit_subjects = find_all_explicit(node, &method(:subject?)) @subject_overrides = find_all_explicit(node, &method(:let?)) find_subject_expectations(node) do |stub| add_offense(stub) end end