Class: RuboCop::Cop::Capybara::RSpec::PredicateMatcher

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
ConfigurableEnforcedStyle, ExplicitHelper, InflectedHelper
Defined in:
lib/rubocop/cop/capybara/rspec/predicate_matcher.rb

Overview

Prefer using predicate matcher over using predicate method directly.

Capybara defines magic matchers for predicate methods. This cop recommends to use the predicate matcher instead of using predicate method directly.

Examples:

Strict: true, EnforcedStyle: inflected (default)

# bad
expect(foo.matches_css?(bar: 'baz')).to be_truthy
expect(foo.matches_selector?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_xpath?(bar: 'baz')).to be_truthy

# good
expect(foo).to match_css(bar: 'baz')
expect(foo).to match_selector(bar: 'baz')
expect(foo).to match_style(bar: 'baz')
expect(foo).to match_xpath(bar: 'baz')

# also good - It checks "true" strictly.
expect(foo.matches_style?(bar: 'baz')).to be(true)

Strict: false, EnforcedStyle: inflected

# bad
expect(foo.matches_style?(bar: 'baz')).to be_truthy
expect(foo.matches_style?(bar: 'baz')).to be(true)

# good
expect(foo).to match_style(bar: 'baz')

Strict: true, EnforcedStyle: explicit

# bad
expect(foo).to match_style(bar: 'baz')

# good - the above code is rewritten to it by this cop
expect(foo.matches_style?(bar: 'baz')).to be(true)

Strict: false, EnforcedStyle: explicit

# bad
expect(foo).to match_style(bar: 'baz')

# good - the above code is rewritten to it by this cop
expect(foo.matches_style?(bar: 'baz')).to be_truthy

Constant Summary collapse

RESTRICT_ON_SEND =
%i[to to_not not_to].freeze

Constants included from ExplicitHelper

ExplicitHelper::BUILT_IN_MATCHERS, ExplicitHelper::INFLECTED_MATCHER, ExplicitHelper::MSG_EXPLICIT

Constants included from InflectedHelper

InflectedHelper::EXPLICIT_MATCHER, InflectedHelper::MSG_INFLECTED

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/rubocop/cop/capybara/rspec/predicate_matcher.rb', line 246

def on_send(node)
  if style == :inflected
    check_inflected(node)
  elsif style == :explicit
    check_explicit(node)
  end
end