Class: RuboCop::Cop::Sevencop::RSpecExamplesInSameGroup

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sevencop/rspec_examples_in_same_group.rb

Overview

Combine examples in same group in the time-consuming kinds of specs.

Examples:

# bad
context 'when user is logged in' do
  it 'returns 200' do
    subject
    expect(response).to have_http_status(200)
  end

  it 'creates Foo' do
    expect { subject }.to change(Foo, :count).by(1)
  end
end

# good
context 'when user is logged in' do
  it 'creates Foo and returns 200' do
    expect { subject }.to change(Foo, :count).by(1)
    expect(response).to have_http_status(200)
  end
end

# bad - IncludeSharedExamples: true
context 'when user is logged in' do
  it 'returns 200' do
    subject
    expect(response).to have_http_status(200)
  end

  includes_examples 'creates Foo'
end

Constant Summary collapse

METHOD_NAMES_FOR_REGULAR_EXAMPLE =
%i[
  example
  it
  its
  scenario
  specify
].to_set.freeze
METHOD_NAMES_FOR_SHARED_EXAMPLES =
%i[
  include_examples
  it_behaves_like
  it_should_behave_like
].to_set.freeze
MSG =
'Combine examples in the same group in the time-consuming kinds of specs.'
RESTRICT_ON_SEND =
[
  *METHOD_NAMES_FOR_REGULAR_EXAMPLE,
  *METHOD_NAMES_FOR_SHARED_EXAMPLES
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ void

This method returns an undefined value.

Parameters:

  • node (RuboCop::AST::SendNode)


62
63
64
65
66
67
68
69
70
71
# File 'lib/rubocop/cop/sevencop/rspec_examples_in_same_group.rb', line 62

def on_send(node)
  node = node.block_node || node

  return unless example?(node)

  previous_sibling_example = previous_sibling_example_of(node)
  return unless previous_sibling_example

  add_offense(node)
end