Class: RuboCop::Cop::RSpec::BeEql
- Defined in:
- lib/rubocop/cop/rspec/be_eql.rb
Overview
Check for expectations where ‘be(…)` can replace `eql(…)`.
The ‘be` matcher compares by identity while the `eql` matcher compares using `eql?`. Integers, floats, booleans, symbols, and nil can be compared by identity and therefore the `be` matcher is preferable as it is a more strict test.
This cop only looks for instances of ‘expect(…).to eql(…)`. We do not check `to_not` or `not_to` since `!eql?` is more strict than `!equal?`. We also do not try to flag `eq` because if `a == b`, and `b` is comparable by identity, `a` is still not necessarily the same type as `b` since the `#==` operator can coerce objects for comparison.
Constant Summary collapse
- MSG =
'Prefer `be` over `eql`.'
Constants inherited from Cop
Cop::DEFAULT_CONFIGURATION, Cop::DEFAULT_PATTERN_RE
Constants included from RSpec::Language
RSpec::Language::ALL, RSpec::Language::RSPEC
Instance Method Summary collapse
Methods inherited from Cop
Instance Method Details
#autocorrect(node) ⇒ Object
51 52 53 |
# File 'lib/rubocop/cop/rspec/be_eql.rb', line 51 def autocorrect(node) ->(corrector) { corrector.replace(node.loc.selector, 'be') } end |
#on_send(node) ⇒ Object
45 46 47 48 49 |
# File 'lib/rubocop/cop/rspec/be_eql.rb', line 45 def on_send(node) eql_type_with_identity(node) do |eql| add_offense(eql, location: :selector) end end |