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

Inherits:
Base
  • Object
show all
Includes:
RuboCop::Cop::RSpec::CapybaraHelp
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', href: 'http://example.com')
expect(page).to have_css('table.cls')
expect(page).to have_css('select')
expect(page).to have_css('input', exact_text: 'foo')

# 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', href: 'http://example.com')
expect(page).to have_table(class: 'cls')
expect(page).to have_select
expect(page).to have_field('foo')

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',
  'input' => 'field'
}.freeze

Instance Method Summary collapse

Methods included from RuboCop::Cop::RSpec::CapybaraHelp

include_option?, replaceable_element?, replaceable_pseudo_class?, replaceable_pseudo_class_not?, replaceable_to_link?, specific_option?, specific_pseudo_classes?

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #numblock_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



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

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

#on_send(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/rspec/capybara/specific_matcher.rb', line 48

def on_send(node)
  first_argument(node) do |arg|
    next unless (matcher = specific_matcher(arg))
    next if CssSelector.multiple_selectors?(arg)
    next unless specific_option?(node, arg, matcher)
    next unless specific_pseudo_classes?(arg)

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