class RuboCop::Cop::RSpec::BeNil

Ensures a consistent style is used when matching ‘nil`.

You can either use the more specific ‘be_nil` matcher, or the more generic `be` matcher with a `nil` argument.

This cop can be configured using the ‘EnforcedStyle` option

@example ‘EnforcedStyle: be_nil` (default)

# bad
expect(foo).to be(nil)

# good
expect(foo).to be_nil

@example ‘EnforcedStyle: be`

# bad
expect(foo).to be_nil

# good
expect(foo).to be(nil)

Constants

BE_MSG
BE_NIL_MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/be_nil.rb, line 45
def on_send(node)
  case style
  when :be
    check_be_style(node)
  when :be_nil
    check_be_nil_style(node)
  end
end

Private Instance Methods

check_be_nil_style(node) click to toggle source
# File lib/rubocop/cop/rspec/be_nil.rb, line 64
def check_be_nil_style(node)
  return unless nil_value_expectation?(node)

  add_offense(node, message: BE_NIL_MSG) do |corrector|
    corrector.replace(node, 'be_nil')
  end
end
check_be_style(node) click to toggle source
# File lib/rubocop/cop/rspec/be_nil.rb, line 56
def check_be_style(node)
  return unless be_nil_matcher?(node)

  add_offense(node, message: BE_MSG) do |corrector|
    corrector.replace(node, 'be(nil)')
  end
end