Class: RuboCop::Cop::RSpec::Dialect

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

Overview

Enforces custom RSpec dialects.

A dialect can be based on the following RSpec methods:

  • describe, context, feature, example_group
  • xdescribe, xcontext, xfeature
  • fdescribe, fcontext, ffeature
  • shared_examples, shared_examples_for, shared_context
  • it, specify, example, scenario, its
  • fit, fspecify, fexample, fscenario, focus
  • xit, xspecify, xexample, xscenario, skip
  • pending
  • prepend_before, before, append_before,
  • around
  • prepend_after, after, append_after
  • let, let!
  • subject, subject!
  • expect, is_expected, expect_any_instance_of
  • raise_error, raise_exception

By default all of the RSpec methods and aliases are allowed. By setting a config like:

RSpec/Dialect:
PreferredMethods:
  context: describe

If you were previously using the RSpec/Capybara/FeatureMethods cop and want to keep disabling all Capybara-specific methods that have the same native RSpec method (e.g. are just aliases), use the following config:

RSpec/Dialect:
PreferredMethods:
  background: :before
  scenario:   :it
  xscenario:  :xit
  given:      :let
  given!:     :let!
  feature:    :describe

You can expect the following behavior:

Examples:

# bad
context 'display name presence' do
  # ...
end

# good
describe 'display name presence' do
  # ...
end

Constant Summary collapse

MSG =
'Prefer `%<prefer>s` over `%<current>s`.'

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

#on_send(node) ⇒ Object



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

def on_send(node)
  return unless rspec_method?(node)
  return unless preferred_methods[node.method_name]

  msg = format(MSG, prefer: preferred_method(node.method_name),
                    current: node.method_name)

  add_offense(node, message: msg) do |corrector|
    current = node.loc.selector
    preferred = preferred_method(current.source)

    corrector.replace(current, preferred)
  end
end

#rspec_method?(node) ⇒ Object



66
# File 'lib/rubocop/cop/rspec/dialect.rb', line 66

def_node_matcher :rspec_method?, '(send #rspec? #ALL.all ...)'