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: ExampleStatementsCount #1125
Comments
Agree. Even for a non-nested structure, e.g.: it_expected.to have_attributes(
a: 1,
b: 2,
c: 3,
d: 4,
e: 5
)
Extracting a part of the matcher into a Do you think setting # spec/.rubocop.yml
Metrics/BlockLength:
IgnoredMethods:
- describe
- context
CountAsOne:
- hash
- array would do? |
How's |
Another option would be |
I do! But YMMV. In my book, this is bad: it 'does blah blah' do
create(:something)
create(:something_else)
get(:foo)
body = last_response.body
data = JSON.parse(body)
expect(data).to now_we_are_talking
end So, ideally, it should be exactly one statement always :) One exception is: it 'sends its messages' do
do_something
expect(something).to have_received(:something)
end I solve it in saharspec with its_block { is_expected.to send_message(something, :something) } ...but that's obviously not for everybody. And actually, my "bad" example is a "typical" example in tutorials, and even on the main of https://rspec.info/ 🤷 |
Related: #249 also we 💯 should add support for CountAsOne in ExampleLength |
Since ExampleLength supports the CountAsOne option, perhaps we can close this? |
Fixed in #1139 |
In my experience,
RSpec/ExampleLength
is somewhat useful cop which, though, frequently misses a point: it constantly flag statements like this (especially in some JSON API testing):I argue that for testing deeply nested structures the code above is the most useful way of writing it :)
When rubocop-rspec flags it, people just move the matcher part into
let
s, making readability and debuggability worse, not better.I thought that maybe what we really want is to limit the number of top-level statements inside an example.
It is not the same as limiting the number of expectations -- the statements might prepare some data before expecting (which, in my book, is a sin on itself, so I'll probably set
ExampleStatementsCount
strictly to 1... But that's just me :)The text was updated successfully, but these errors were encountered: