Class: RuboCop::Cop::RSpec::ExampleWithoutDescription

Inherits:
Base
  • Object
show all
Includes:
ConfigurableEnforcedStyle
Defined in:
lib/rubocop/cop/rspec/example_without_description.rb

Overview

Checks for examples without a description.

RSpec allows for auto-generated example descriptions when there is no description provided or the description is an empty one. It is acceptable to use ‘specify` without a description

This cop removes empty descriptions. It also defines whether auto-generated description is allowed, based on the configured style.

This cop can be configured using the ‘EnforcedStyle` option

Examples:

# always good
specify do
  result = service.call
  expect(result).to be(true)
end

‘EnforcedStyle: always_allow` (default)

# bad
it('') { is_expected.to be_good }
specify '' do
  result = service.call
  expect(result).to be(true)
end

# good
it { is_expected.to be_good }
specify do
  result = service.call
  expect(result).to be(true)
end

‘EnforcedStyle: single_line_only`

# bad
it('') { is_expected.to be_good }
it do
  result = service.call
  expect(result).to be(true)
end

# good
it { is_expected.to be_good }

‘EnforcedStyle: disallow`

# bad
it { is_expected.to be_good }
it do
  result = service.call
  expect(result).to be(true)
end

Constant Summary collapse

MSG_DEFAULT_ARGUMENT =
'Omit the argument when you want to ' \
'have auto-generated description.'
MSG_ADD_DESCRIPTION =
'Add a description.'

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#example_description(node) ⇒ Object



67
# File 'lib/rubocop/cop/rspec/example_without_description.rb', line 67

def_node_matcher :example_description, '(send nil? _ $(str $_))'

#on_block(node) ⇒ Object

rubocop:disable InternalAffairs/NumblockHandler



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubocop/cop/rspec/example_without_description.rb', line 69

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  return unless example?(node)

  check_example_without_description(node.send_node)

  example_description(node.send_node) do |message_node, message|
    return unless message.to_s.empty?

    add_offense(message_node, message: MSG_DEFAULT_ARGUMENT)
  end
end