class RuboCop::Cop::RSpec::PredicateMatcher

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.

@example 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).to be(true)

@example Strict: false, EnforcedStyle: inflected

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

# good
expect(foo).to be_something

@example 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)

@example 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

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 310
def on_block(node)
  check_explicit(node) if style == :explicit
end
on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 301
def on_send(node)
  case style
  when :inflected
    check_inflected(node)
  when :explicit
    check_explicit(node)
  end
end

Private Instance Methods

args_loc(send_node) click to toggle source

returns args location with whitespace @example

foo 1, 2
   ^^^^^
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 329
def args_loc(send_node)
  range_between(send_node.loc.selector.end_pos,
                send_node.loc.expression.end_pos)
end
autocorrect(node) click to toggle source
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 316
def autocorrect(node)
  case style
  when :inflected
    autocorrect_inflected(node)
  when :explicit
    autocorrect_explicit(node)
  end
end
block_loc(send_node) click to toggle source

returns block location with whitespace @example

foo { bar }
   ^^^^^^^^
# File lib/rubocop/cop/rspec/predicate_matcher.rb, line 338
def block_loc(send_node)
  parent = send_node.parent
  return unless parent.block_type?
  range_between(
    send_node.loc.expression.end_pos,
    parent.loc.expression.end_pos
  )
end