Class: RuboCop::Cop::RSpec::Capybara::FeatureMethods

Inherits:
RuboCop::Cop::RSpec::Cop show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/capybara/feature_methods.rb

Overview

Checks for consistent method usage in feature specs.

By default, the cop disables all Capybara-specific methods that have the same native RSpec method (e.g. are just aliases). Some teams however may prefer using some of the Capybara methods (like `feature`) to make it obvious that the test uses Capybara, while still disable the rest of the methods, like `given` (alias for `let`), `background` (alias for `before`), etc. You can configure which of the methods to be enabled by using the EnabledMethods configuration option.

Examples:

# bad
feature 'User logs in' do
  given(:user) { User.new }

  background do
    visit new_session_path
  end

  scenario 'with OAuth' do
    # ...
  end
end

# good
describe 'User logs in' do
  let(:user) { User.new }

  before do
    visit new_session_path
  end

  it 'with OAuth' do
    # ...
  end
end

Constant Summary collapse

MSG =
'Use `%<replacement>s` instead of `%<method>s`.'
MAP =
{
  background: :before,
  scenario:   :it,
  xscenario:  :xit,
  given:      :let,
  given!:     :let!,
  feature:    :describe
}.freeze

Constants inherited from RuboCop::Cop::RSpec::Cop

RuboCop::Cop::RSpec::Cop::DEFAULT_CONFIGURATION, RuboCop::Cop::RSpec::Cop::DEFAULT_PATTERN_RE

Constants included from RSpec::Language

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

Instance Method Summary collapse

Methods inherited from RuboCop::Cop::RSpec::Cop

inherited, #relevant_file?

Instance Method Details

#message(range) ⇒ Object


82
83
84
85
# File 'lib/rubocop/cop/rspec/capybara/feature_methods.rb', line 82

def message(range)
  name = range.source.to_sym
  format(MSG, method: name, replacement: MAP[name])
end

#on_block(node) ⇒ Object


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

def on_block(node)
  return unless inside_spec?(node)

  feature_method(node) do |send_node, match|
    next if enabled?(match)

    add_offense(send_node.loc.selector) do |corrector|
      corrector.replace(send_node.loc.selector, MAP[match].to_s)
    end
  end
end