Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve RSpec/SubjectStub cop #770

Merged
merged 10 commits into from Jun 10, 2019

Remove special case `all` matcher from SubjectStub

This special case treatment was added to address one use case,
of an object that acts as a container for and a delegator of
method calls to containing items by inheriting from an `Array`.

The original code that suffered from a false detection was:

    expect(formatter_set).to all(receive(:started).with(files))
    formatter_set.started(files)

However, it has been [reworked to](https://github.com/rubocop-hq/rubocop/blob/a0aaa233c20e18784d841212e1caa724bcc919f7/spec/rubocop/formatter/formatter_set_spec.rb#18):

    expect(formatter_set[0]).to receive(:started).with(files)
    expect(formatter_set[1]).to receive(:started).with(files)
    formatter_set.started(files)

and all known cases for this specific offence case are gone.
  • Loading branch information...
pirj committed May 15, 2019
commit f06446f238055b51f5460615302fa31537b8f864
@@ -58,32 +58,21 @@ class SubjectStub < Cop
# expect(foo).to receive(:bar).with(1)
# expect(foo).to receive(:bar).with(1).and_return(2)
#
# @example source that not matches
# expect(foo).to all(receive(:bar))
#
def_node_matcher :message_expectation?, <<-PATTERN
(send
{
(send nil? { :expect :allow } (send nil? %))
(send nil? :is_expected)
}
#{Runners::ALL.node_pattern_union}
#expectation?
#message_expectation_matcher?
)
PATTERN

def_node_matcher :all_matcher?, '(send nil? :all ...)'

def_node_search :receive_message?, <<-PATTERN
def_node_search :message_expectation_matcher?, <<-PATTERN
(send nil? { :receive :receive_messages :receive_message_chain } ...)
PATTERN

def expectation?(node)
return if all_matcher?(node)

receive_message?(node)
end

def on_block(node)
return unless example_group?(node)

@@ -72,12 +72,13 @@
RUBY
end

it 'ignores stub when inside all matcher' do
expect_no_offenses(<<-RUBY)
it 'flags stub inside all matcher' do
expect_offense(<<-RUBY)
describe Foo do
subject(:foo) { [Object.new] }
it 'tries to trick rubocop-rspec' do
expect(foo).to all(receive(:baz))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not stub methods of the object under test.
end
end
RUBY
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.