rubocop-rspec
rubocop-rspec copied to clipboard
False-positive RSpec/RepeatedDescription when using rspec-its
Continuing https://github.com/rubocop/rubocop-rspec/issues/879. We're using rspec-its and have examples like this:
describe Service do
describe 'returned values' do
subject { service }
before { service.call }
its(:model) { is_expected.not_to be_valid }
its(:model) { is_expected.not_to be_persisted }
end
end
Rubocop raises this error on each its(:model) line: C: RSpec/RepeatedDescription: Don't repeat descriptions within an example group.
I believe this is not correct, since RSpec generated different descriptions for every example based on example's body:
Service
returned values
model
is expected not to be valid
model
is expected not to be persisted
So ideally, in case of examples defined with rspec-its, example's body should be the source of this uniqueness check, but not the its(...) definitions which may look equal.
Fair enough, especially given that negated expectations are not trivial to combine into one.
Would you like to submit a PR? I'd expect:
- Taking parts from
lib/rubocop/cop/rspec/repeated_example.rbthat adds some unique example attribute (location?) foritsto effectively ignore duplicate attribute names. - Add an example to
spec/rubocop/cop/rspec/repeated_example_spec.rbto make sure duplicates are detected (passed with no changes required to the cop):
it 'does not flag examples when different its arguments are used' do
expect_offense(<<-RUBY)
describe 'doing x' do
its(:x) { is_expected.to be_present }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't repeat examples within an example group.
its(:x) { is_expected.to be_present }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Don't repeat examples within an example group.
end
RUBY
end
I withdraw my proposal of introducing IgnoreItsWithSameAttribute, as it seems to make sense to make this the default.
Note for the future researchers. Even though its accepts arbitrary arguments, the "docstring" of the generated example is explicitly set to nil, so all of those arguments can be treated as metadata.
Would you like to submit a PR fixing this, @arg ?
I will try to find some time, but can’t say when.
On 26 May 2022, at 21:57, Phil Pirozhkov @.***> wrote:
Would you like to submit a PR fixing this, @arg ?
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.
Ping @arg
If you are still busy, I would like to work on this.
That would be great
On 24 Oct 2022, at 11:07, Yudai Takada @.***> wrote:
If you are still busy, I would like to work on this.
— Reply to this email directly, view it on GitHub https://github.com/rubocop/rubocop-rspec/issues/1273#issuecomment-1288693965, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH66NOCKHIHFPIA63YOFMTWEZGTJANCNFSM5WHSZ7EQ. You are receiving this because you were mentioned.