class RuboCop::Cop::Capybara::NegationMatcher
expect(page).not_to have_css(‘a’)
expect(page).not_to have_selector ‘a’
# good
expect(page).to have_no_css(‘a’)
expect(page).to have_no_selector ‘a’
# bad
@example EnforcedStyle: not_to
expect(page).to have_no_css(‘a’)
expect(page).to have_no_selector ‘a’
# good
expect(page).not_to have_css(‘a’)
expect(page).not_to have_selector ‘a’
# bad
@example EnforcedStyle: have_no (default)
Enforces use of ‘have_no_*` or `not_to` for negated expectations.
def message(matcher)
def message(matcher) format(MSG, runner: replaced_runner, matcher: replaced_matcher(matcher)) end
def offense?(node)
def offense?(node) node.arguments? && ((style == :have_no && not_to?(node.parent)) || (style == :not_to && have_no?(node.parent))) end
def offense_range(node)
def offense_range(node) node.parent.loc.selector.with(end_pos: node.loc.selector.end_pos) end
def on_send(node)
def on_send(node) return unless offense?(node) matcher = node.method_name.to_s add_offense(offense_range(node), message: message(matcher)) do |corrector| corrector.replace(node.parent.loc.selector, replaced_runner) corrector.replace(node.loc.selector, replaced_matcher(matcher)) end end
def replaced_matcher(matcher)
def replaced_matcher(matcher) case style when :have_no matcher.sub('have_', 'have_no_') when :not_to matcher.sub('have_no_', 'have_') else # :nocov: :noop # :nocov: end end
def replaced_runner
def replaced_runner case style when :have_no 'to' when :not_to 'not_to' else # :nocov: :noop # :nocov: end end