rubocop-rspec icon indicating copy to clipboard operation
rubocop-rspec copied to clipboard

False-positive RSpec/RepeatedDescription when using rspec-its

Open arg opened this issue 3 years ago • 5 comments
trafficstars

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.

arg avatar May 18 '22 08:05 arg

Fair enough, especially given that negated expectations are not trivial to combine into one.

Would you like to submit a PR? I'd expect:

  1. Taking parts from lib/rubocop/cop/rspec/repeated_example.rb that adds some unique example attribute (location?) for its to effectively ignore duplicate attribute names.
  2. Add an example to spec/rubocop/cop/rspec/repeated_example_spec.rb to 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

pirj avatar May 18 '22 09:05 pirj

I withdraw my proposal of introducing IgnoreItsWithSameAttribute, as it seems to make sense to make this the default.

pirj avatar May 18 '22 09:05 pirj

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.

pirj avatar May 18 '22 09:05 pirj

Would you like to submit a PR fixing this, @arg ?

pirj avatar May 26 '22 19:05 pirj

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.

arg avatar May 28 '22 07:05 arg

Ping @arg

pirj avatar Oct 15 '22 14:10 pirj

If you are still busy, I would like to work on this.

ydah avatar Oct 24 '22 09:10 ydah

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.

arg avatar Oct 24 '22 09:10 arg