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

RSpec/IteratedExpectation complains about code that can't be rewritten to use all #1206

Closed
ccutrer opened this issue Nov 1, 2021 · 3 comments

Comments

@ccutrer
Copy link

ccutrer commented Nov 1, 2021

dont_convert.each do |ascii|
  expect(ascii).to eq LuckySneaks::Unidecoder.decode(ascii)
end

RSpec/IteratedExpectation: Prefer using the all matcher instead of iterating over an array.

I'd love to convert to an all, but afaict all can't modify the expectation per element, since the expectation is dynamic based on the element. I was hoping for a syntax like

expect(dont_convert).to(all { |ascii| eq LuckySneaks::Unidecoder.decode(ascii) }) but it doesn't work.

rubocop -V:

1.22.0 (using Parser 3.0.2.0, rubocop-ast 1.12.0, running on ruby 2.7.2 x86_64-darwin20)
  - rubocop-performance 1.11.5
  - rubocop-rails 2.12.4
  - rubocop-rake 0.6.0
  - rubocop-rspec 2.5.0
@pirj
Copy link
Member

pirj commented Nov 2, 2021

Well, this specific case can be converted with satisfy:

expect(data_convert).to all satisfy { |ascii| ascii == LuckySneaks::Unidecoder.decode(ascii) }

@pirj pirj closed this as completed Nov 2, 2021
@ccutrer
Copy link
Author

ccutrer commented Nov 2, 2021

Perfect! Thanks! You might want to include an example of converting to satisfy in the docs for the cop. I've never seen it before. One of my favorite things about rubocop is that it points me to learn knew things about writing better code that I never knew existed.

@pirj
Copy link
Member

pirj commented Nov 2, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants