Class: RuboCop::Cop::Capybara::NegationMatcherAfterVisit

Inherits:
Base
  • Object
show all
Includes:
CapybaraHelp
Defined in:
lib/rubocop/cop/capybara/negation_matcher_after_visit.rb

Overview

Do not allow negative matchers to be used immediately after ‘visit`.

Examples:

# bad
visit foo_path
expect(page).to have_no_link('bar')
expect(page).to have_css('a')

# good
visit foo_path
expect(page).to have_css('a')
expect(page).to have_no_link('bar')

# bad
visit foo_path
expect(page).not_to have_link('bar')
expect(page).to have_css('a')

# good
visit foo_path
expect(page).to have_css('a')
expect(page).not_to have_link('bar')

Constant Summary collapse

MSG =
'Do not use negation matcher immediately after visit.'
RESTRICT_ON_SEND =
%i[visit].freeze

Constants included from CapybaraHelp

CapybaraHelp::CAPYBARA_MATCHERS, CapybaraHelp::COMMON_OPTIONS, CapybaraHelp::NEGATIVE_MATCHERS, CapybaraHelp::POSITIVE_MATCHERS, CapybaraHelp::SPECIFIC_OPTIONS, CapybaraHelp::SPECIFIC_PSEUDO_CLASSES

Instance Method Summary collapse

Methods included from CapybaraHelp

common_attributes?, include_option?, replaceable_attributes?, replaceable_element?, replaceable_option?, replaceable_pseudo_class?, replaceable_pseudo_class_not?, replaceable_pseudo_classes?, replaceable_to_link?

Instance Method Details

#negation_matcher?(node) ⇒ Object

[View source]

36
37
38
39
40
41
# File 'lib/rubocop/cop/capybara/negation_matcher_after_visit.rb', line 36

def_node_matcher :negation_matcher?, <<~PATTERN
  {
    (send (send nil? :expect _) :to (send nil? %NEGATIVE_MATCHERS ...))
    (send (send nil? :expect _) :not_to (send nil? %POSITIVE_MATCHERS ...))
  }
PATTERN

#on_send(node) ⇒ Object

[View source]

43
44
45
46
47
# File 'lib/rubocop/cop/capybara/negation_matcher_after_visit.rb', line 43

def on_send(node)
  negation_matcher?(node.right_sibling) do
    add_offense(node.right_sibling)
  end
end