rubocop / rubocop-rspec

Code style checking for RSpec files.
https://docs.rubocop.org/rubocop-rspec
MIT License
794 stars 272 forks source link

Cop idea: Prevent using expect in let. #1868

Closed marocchino closed 1 month ago

marocchino commented 3 months ago

I've noticed that some people use let as an escape from the before hook to avoid RSpec/ExpectInHook.

so..

  let(:expect_let) do
    expect(Module).to receive(:call)
  end

  before do
    expect_let
  end

will not trigger RSpec/ExpectInHook.

There is no good reason to put expect inside let in the first place, so I think it would be better to disallow it.

marocchino commented 1 month ago

https://github.com/betterspecs/betterspecs/issues/233 Maybe related this issue.

marocchino commented 1 month ago

I will make pr part of RSpec/ExpectInHook as a let containing expect is called in before.

pirj commented 1 month ago

If those people deliberately put expectations inside let’s to escape a cop, what would prevent them from defining a method like ‘def expect_foo’ and using it inside a ‘before’ hook?

I don’t think that our goal is to follow and attempt to flag all those nonsensical usages. We’re more like to enforce the style guide plus help enforce a given styling if several equally acceptable styles exist plus help people avoiding common mistakes. Making an effort against deliberate abuse seems overkill.

pirj commented 1 month ago

BetterSpecs is dead, long live rspec-style-guide that absorbed all good things from betterspecs.