Class: RuboCop::Cop::RSpec::Be

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

Overview

Check for expectations where ‘be` is used without argument.

The ‘be` matcher is too generic, as it pass on everything that is not nil or false. If that is the exact intend, use `be_truthy`. In all other cases it’s better to specify what exactly is the expected value.

Examples:

# bad
expect(foo).to be

# good
expect(foo).to be_truthy
expect(foo).to be 1.0
expect(foo).to be(true)

Constant Summary collapse

MSG =
"Don't use `be` without an argument."
RESTRICT_ON_SEND =
Runners.all

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#be_without_args(node) ⇒ Object



27
28
29
# File 'lib/rubocop/cop/rspec/be.rb', line 27

def_node_matcher :be_without_args, <<~PATTERN
  (send _ #Runners.all $(send nil? :be))
PATTERN

#on_send(node) ⇒ Object



31
32
33
34
35
# File 'lib/rubocop/cop/rspec/be.rb', line 31

def on_send(node)
  be_without_args(node) do |matcher|
    add_offense(matcher.loc.selector)
  end
end