rubocop-rspec: `RSpec/NoExpectationExample` false positives
scenario 'test' do
method
end
private
def method
expect(1).to eq(1)
end
This gives RSpec/NoExpectationExample but it shouldn’t. 😃
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 24 (17 by maintainers)
Commits related to this issue
- Disable broken RuboCop-RSpec cop https://github.com/rubocop/rubocop-rspec/issues/1385 — committed to AlexWayfer/sequel-enum_values by AlexWayfer 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to ydah/rubocop-rspec by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to rubocop/rubocop-capybara by ydah 2 years ago
- Add `AllowedPatterns` configuration option to `RSpec/NoExpectationExample` Fix: https://github.com/rubocop/rubocop-rspec/issues/1385 This cop can be customized allowed expectation methods pattern wi... — committed to rubocop/rubocop-factory_bot by ydah 2 years ago
I have reviewed a codebase with the cop applied, and now I think that we may add an
AllowedPatternsconfiguration, so people can just whitelistexpect_andassert_methods without needing to add them to Language. Though having not them in language also forces people to think how to better express them, e.g. creating custom matchers, instead of merely methods that run expectations@annaswims you can add
assert_text(and other Capybara assertions) in the language config, so it counts it as an expectationPersonally, I follow the recommendation to use helper methods that is supported by the Effective Testing with RSpec book.
However, since for static checking it creates complications, do you think we can all agree that if something is an expectation, we can name it as such?
In such a way, we can introduce an option with a default to the cop, say,
ConsiderHelperMethodsPrefixes: [expect]that would consider such helper methods as expectations.Eventually, this can become part of the community RSpec style guide if it gets some traction.
How does that sound?
PS My apologies for an attempt to steal the idea, this is what @Darhazer suggests above.
A lot of edge cases for single cop, maybe it should be disabled by default
It also does not accept
Capybara::Node::Matchers#assert_textas an assertion in an example like:If no one plans to send a PR, I will send. How do you like it?
As I can see from the source of e.g.
has_link?,just returns
falsethat is not sufficient for RSpec to consider the example failed.Won’t
be equivalent to:
?
As I recall how RSpec predicate matchers work,
expect(page).to have_link(...)is a just another way of writingexpect(page.has_link?(...)).to be(true). But omittingexpect(page)effectively means no expectation is made.Similar problem then expect in
afterblockSomething like this:
There’s no better candidate than you, @ydah with so many recent contributions, please go for it.
Does anyone want to send a PR to introduce
AllowedPatternsconfiguration option, so with defaultexpect_andassert_as @Darhazer suggests above?RSpec/ExpectInHook disapproves such style, @ShockwaveNN
Yep, @pirj ,you’re right - I don’t know why I was thinking these matchers are working also like expectations 😦
I think we have to account for the expectations in hooks that are in scope. We have the
ExpectInHookthat would disallow such usage, but it would be frustrating for users to disable that cop just to haveNoExpectationExamplecomplain again.Maybe we also should add the capybara expectations by default to the language (perhaps behind a config option). I’m on a fense for this one, as it would affect things like
MultipleExpectations- perhaps the style for capybara scenarios is a bit different.@rubocop/rubocop-rspec WDYT?