Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cop idea: Replace equal with be #1676

Open
ydakuka opened this issue Jul 31, 2023 · 5 comments
Open

Cop idea: Replace equal with be #1676

ydakuka opened this issue Jul 31, 2023 · 5 comments
Labels

Comments

@ydakuka
Copy link

ydakuka commented Jul 31, 2023

Reference: https://github.com/rspec/rspec-expectations#identity

Actual behavior

I have the following spec:

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ApplicationHelper do
  specify do
    expect(foo).to equal described_class
  end

  def foo
    described_class
  end
end

I run rubocop and don't get any offences:

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop spec/helpers/application_helper_spec.rb 
Inspecting 1 file
.

1 file inspected, no offenses detected

Expected behavior

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ApplicationHelper do
  specify do
    expect(foo).to be described_class
  end

  def foo
    described_class
  end
end

Rubocop

ydakuka@yauhenid:~/Work/project$ bin/rails_docker rubocop -V
1.54.1 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 2.7.8) [x86_64-linux]
  - rubocop-capybara 2.18.0
  - rubocop-factory_bot 2.23.1
  - rubocop-performance 1.18.0
  - rubocop-rails 2.20.2
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.22.0
  - rubocop-thread_safety 0.5.1
@pirj
Copy link
Member

pirj commented Jul 31, 2023

Both matchers exist in RSpec. Are they always interchangeable?
Why one should be used instead of another?

@ydakuka
Copy link
Author

ydakuka commented Jul 31, 2023

Are they always interchangeable?

If any arguments pass, the matchers will be equal. https://github.com/rspec/rspec-expectations/blob/main/lib/rspec/matchers.rb#L349

Why one should be used instead of another?

In my opinion the be matcher is used oftener, while the equal matcher is used less frequently. (https://github.com/rspec/rspec-expectations/tree/main#comparisons , https://github.com/rspec/rspec-expectations/tree/main#truthiness)

@pirj
Copy link
Member

pirj commented Jul 31, 2023

be matcher is used oftener

Can you prove that by harvesting real-world-rspec? I’d specifically be interested in cases where the argument is not among nil/true/false.

@bquorning
Copy link
Collaborator

Related: #933.

@ydakuka
Copy link
Author

ydakuka commented Aug 2, 2023

Can you prove that by harvesting real-world-rspec? I’d specifically be interested in cases where the argument is not among nil/true/false.

@pirj I've searched in real-world-rspec and got the following results:

Searching 54609 files for "(?!.*nil|.*false|.*true)(\.to_not equal(\ |\(|\_).{1,})" (regex)
10 matches across 6 files

Searching 54609 files for "(?!.*nil|.*false|.*true)(\.to equal(\ |\(|\_).{1,})" (regex)
595 matches across 157 files

Searching 54609 files for "(?!.*nil|.*false|.*true)(\.not_to equal(\ |\(|\_).{1,})" (regex)
39 matches across 20 files

and

Searching 54609 files for "(?!.*nil|.*false|.*true)(\.to_not be(\ |\(|\_).{1,})" (regex)
672 matches across 247 files

Searching 54609 files for "(?!.*nil|.*false|.*true)(\.to be(\ |\(|\_).{1,})" (regex)
35901 matches across 6329 files

Searching 54609 files for "(?!.*nil|.*false|.*true)(\.not_to be(\ |\(|\_).{1,})" (regex)
6345 matches across 1752 files

@ydah ydah added the cop label Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants