Class: RuboCop::Cop::RSpec::AvoidBeTruthy

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/avoid_be_truthy.rb

Overview

Examples:

# bad
expect(subject).to be_truthy

# good
expect(subject).to be true

# good
expect(subject.nil).to be false

Constant Summary collapse

MSG =
"Use `#be true` instead of `#be_truthy`."
RESTRICT_ON_SEND =
i[be_truthy].freeze

Instance Method Summary collapse

Instance Method Details

#be_truthy?(node) ⇒ Object



25
26
27
# File 'lib/rubocop/cop/rspec/avoid_be_truthy.rb', line 25

def_node_matcher :be_truthy?, "(send nil? :be_truthy ...)\n"

#on_send(node) ⇒ Object Also known as: on_csend

Called on every ‘send` node (method call) while walking the AST. TODO: remove this method if inspecting `send` nodes is unneeded for your cop. By default, this is aliased to `on_csend` as well to handle method calls with safe navigation, remove the alias if this is unnecessary. If kept, ensure your tests cover safe navigation as well!



34
35
36
37
38
# File 'lib/rubocop/cop/rspec/avoid_be_truthy.rb', line 34

def on_send(node)
  return unless be_truthy?(node)

  add_offense(node)
end