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

Inherits:
Cop
  • Object
show all
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

Constants inherited from Cop

Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

RSpec::Language::ALL, RSpec::Language::RSPEC

Instance Method Summary collapse

Methods inherited from Cop

inherited, #relevant_file?

Instance Method Details

#autocorrect(range) ⇒ Object



54
55
56
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 54

def autocorrect(range)
  ->(corrector) { corrector.replace(range, replacement_text(range)) }
end

#on_block(node) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/rubocop/cop/rspec/example_wording.rb', line 44

def on_block(node)
  it_description(node) do |description_node, message|
    if message =~ SHOULD_PREFIX
      add_wording_offense(description_node, MSG_SHOULD)
    elsif message =~ IT_PREFIX
      add_wording_offense(description_node, MSG_IT)
    end
  end
end