class RuboCop::Cop::RSpec::Capybara::FeatureMethods

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.

@example

# 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

Constants

MAP

git.io/v7Kwr

MSG

Public Instance Methods

message(range) click to toggle source
# File lib/rubocop/cop/rspec/capybara/feature_methods.rb, line 89
def message(range)
  name = range.source.to_sym
  format(MSG, method: name, replacement: MAP[name])
end
on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/capybara/feature_methods.rb, line 77
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

Private Instance Methods

enabled?(method_name) click to toggle source
# File lib/rubocop/cop/rspec/capybara/feature_methods.rb, line 111
def enabled?(method_name)
  enabled_methods.include?(method_name)
end
enabled_methods() click to toggle source
# File lib/rubocop/cop/rspec/capybara/feature_methods.rb, line 115
def enabled_methods
  cop_config
    .fetch('EnabledMethods', [])
    .map(&:to_sym)
end
inside_spec?(node) click to toggle source
# File lib/rubocop/cop/rspec/capybara/feature_methods.rb, line 96
def inside_spec?(node)
  return spec?(node) if root_node?(node)

  root = node.ancestors.find { |parent| root_node?(parent) }
  spec?(root)
end
root_node?(node) click to toggle source
# File lib/rubocop/cop/rspec/capybara/feature_methods.rb, line 103
def root_node?(node)
  node.parent.nil? || root_with_siblings?(node.parent)
end
root_with_siblings?(node) click to toggle source
# File lib/rubocop/cop/rspec/capybara/feature_methods.rb, line 107
def root_with_siblings?(node)
  node.begin_type? && node.parent.nil?
end