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

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

Overview

Prefer using predicate matcher over using predicate method directly.

RSpec 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.something?).to be_truthy

# good
expect(foo).to be_something

# also good - It checks "true" strictly.
expect(foo.something?).to be(true)

Strict: false, EnforcedStyle: inflected

# bad
expect(foo.something?).to be_truthy
expect(foo.something?).to be(true)

# good
expect(foo).to be_something

Strict: true, EnforcedStyle: explicit

# bad
expect(foo).to be_something

# good - the above code is rewritten to it by this cop
expect(foo.something?).to be(true)

Strict: false, EnforcedStyle: explicit

# bad
expect(foo).to be_something

# good - the above code is rewritten to it by this cop
expect(foo.something?).to be_truthy

Constant Summary

Constants included from ExplicitHelper

ExplicitHelper::BUILT_IN_MATCHERS, ExplicitHelper::MSG_EXPLICIT

Constants included from InflectedHelper

InflectedHelper::MSG_INFLECTED

Instance Method Summary collapse

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Methods inherited from Base

inherited, #on_new_investigation

Instance Method Details

#on_block(node) ⇒ Object


289
290
291
# File 'lib/rubocop/cop/rspec/predicate_matcher.rb', line 289

def on_block(node)
  check_explicit(node) if style == :explicit
end

#on_send(node) ⇒ Object


280
281
282
283
284
285
286
287
# File 'lib/rubocop/cop/rspec/predicate_matcher.rb', line 280

def on_send(node)
  case style
  when :inflected
    check_inflected(node)
  when :explicit
    check_explicit(node)
  end
end