Class: RuboCop::Cop::RSpec::Capybara::SpecificMatcher

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/capybara/specific_matcher.rb

Overview

Checks for there is a more specific matcher offered by Capybara.

Examples:


# bad
expect(page).to have_selector('button')
expect(page).to have_no_selector('button.cls')
expect(page).to have_css('button')
expect(page).to have_no_css('a.cls', exact_text: 'foo')
expect(page).to have_css('table.cls')
expect(page).to have_css('select')

# good
expect(page).to have_button
expect(page).to have_no_button(class: 'cls')
expect(page).to have_button
expect(page).to have_no_link('foo', class: 'cls')
expect(page).to have_table(class: 'cls')
expect(page).to have_select

Constant Summary collapse

MSG =
'Prefer `%<good_matcher>s` over `%<bad_matcher>s`.'
RESTRICT_ON_SEND =
%i[have_selector have_no_selector have_css
have_no_css].freeze
SPECIFIC_MATCHER =
{
  'button' => 'button',
  'a' => 'link',
  'table' => 'table',
  'select' => 'select'
}.freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#first_argument(node) ⇒ Object


39
40
41
# File 'lib/rubocop/cop/rspec/capybara/specific_matcher.rb', line 39

def_node_matcher :first_argument, <<-PATTERN
  (send nil? _ (str $_) ... )
PATTERN

#on_send(node) ⇒ Object


43
44
45
46
47
48
49
# File 'lib/rubocop/cop/rspec/capybara/specific_matcher.rb', line 43

def on_send(node)
  return unless (arg = first_argument(node))
  return unless (matcher = specific_matcher(arg))
  return if acceptable_pattern?(arg)

  add_offense(node, message: message(node, matcher))
end