class RuboCop::Cop::RSpec::ContextWording

`context` block descriptions should start with 'when', or 'with'.

@see github.com/reachlocal/rspec-style-guide#context-descriptions @see www.betterspecs.org/#contexts

@example `Prefixes` configuration option, defaults: 'when', 'with', and 'without'

Prefixes:
  - when
  - with
  - without
  - if

@example

# bad
context 'the display name not present' do
  # ...
end

# good
context 'when the display name is not present' do
  # ...
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 36
def on_block(node)
  context_wording(node) do |context|
    add_offense(context, message: message)
  end
end

Private Instance Methods

bad_prefix?(description) click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 44
def bad_prefix?(description)
  !prefixes.include?(description.split.first)
end
joined_prefixes() click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 56
def joined_prefixes
  quoted = prefixes.map { |prefix| "'#{prefix}'" }
  return quoted.first if quoted.size == 1

  quoted << "or #{quoted.pop}"
  quoted.join(', ')
end
message() click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 52
def message
  format(MSG, prefixes: joined_prefixes)
end
prefixes() click to toggle source
# File lib/rubocop/cop/rspec/context_wording.rb, line 48
def prefixes
  cop_config['Prefixes'] || []
end