Class: RuboCop::Cop::RSpec::ExampleWording

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/example_wording.rb

Overview

Checks for common mistakes in example descriptions.

This cop will correct docstrings that begin with 'should' and 'it'.

The autocorrect is experimental - use with care! It can be configured with CustomTransform (e.g. have => has) and IgnoredWords (e.g. only).

Examples:

# bad
it 'should find nothing' do
end

# good
it 'finds nothing' do
end
# bad
it 'it does things' do
end

# good
it 'does things' do
end

See Also:

Constant Summary collapse

MSG_SHOULD =
'Do not use should when describing your tests.'
MSG_IT =
"Do not repeat 'it' when describing your tests."
SHOULD_PREFIX =
/\Ashould(?:n't)?\b/i.freeze
IT_PREFIX =
/\Ait /i.freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_block(node) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 48

def on_block(node)
  it_description(node) do |description_node, message|
    if message.match?(SHOULD_PREFIX)
      add_wording_offense(description_node, MSG_SHOULD)
    elsif message.match?(IT_PREFIX)
      add_wording_offense(description_node, MSG_IT)
    end
  end
end