Rubocop-rspec: Bug in RSpec/RepeatedDescription

Created on 11 Feb 2020  Â·  13Comments  Â·  Source: rubocop-hq/rubocop-rspec

In v1.38.0 (after #874), RSpec/RepeatedDescription started reporting “Don't repeat descriptions within an example group” on descriptions consisting of interpolated strings.

Example of a failing spec:

  it 'does not flag similar descriptions with different interpolated variables' do
    expect_no_offenses(<<-RUBY)
      describe 'doing x' do
        it "does #{x}" do
        end

        it "does #{y}" do
        end
      end
    RUBY
  end

As far as I can tell, there are two problems involved:

  1. RuboCop::RSpec::Example#doc_string only considering str and not dstr nodes in https://github.com/rubocop-hq/rubocop-rspec/blob/v1.38.0/lib/rubocop/rspec/example.rb#L7
  2. signatures.any? return true for [[], nil] in https://github.com/rubocop-hq/rubocop-rspec/blob/v1.38.0/lib/rubocop/cop/rspec/repeated_description.rb#L64
bug

All 13 comments

@lazycoder9 Looks similar to how you've covered it in RepeatedExampleGroupDescription. Do you have time to take a look?

@pirj yeah, pretty similar, will look into it ASAP

Getting this issue too since the last update. Example:

%i[foo bar].each do |type|
  it "does a thing #{type}" do
    ...
  end
end
C: RSpec/RepeatedDescription: Don't repeat descriptions within an example group.
  it "does a thing #{type}" do
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Wondering if it's a separate case or the same problem.
Are there any other similar example description in the same example group, or just this one inside the interation @jimbali ?

It's actually more like:

%i[foo bar].each do |type|
  it "does a thing #{type}" do
    ...
  end
end

%i[foz baz].each do |type|
  it "does a thing #{type}" do
    ...
  end
end

I think it’s the str vs dstr issue I mentioned. I have a failing case like this:

describe "foo" do
  it "bar #{x}" do
    expect(x).to eq(2)
  end

  it "baz #{y}" do
    expect(y).to eq(3)
  end
end
C: RSpec/RepeatedDescription: Don't repeat descriptions within an example group.
    it "bar #{x}" do
    ^^^^^^^^^^^^^
C: RSpec/RepeatedDescription: Don't repeat descriptions within an example group.
    it "baz #{y}" do
    ^^^^^^^^^^^^^

Let's make sure the ones inside iterators are distinguished properly, even if docstrings are identical.

@pirj @bquorning locally, I've fixed issue with dstr and cases like

describe "foo" do
  it "bar #{x}" do
    expect(x).to eq(2)
  end

  it "baz #{y}" do
    expect(y).to eq(3)
  end
end

does not registers offenses. But I wonder how examples inside iterators can be solved (or even should be solved)? But it is OK for a case like this

describe 'doing x' do
  %i[foo bar].each do |type|
    it "does a thing \#{type}" do
    end
  end
end

describe 'doing x' do
  %i[foz baz].each do |type|
    it "does a thing \#{type}" do
    end
  end
end

WDYT?

I see what you mean. In your code example the example group descriptions ("doing x") are identical, and the code _could_ be written as

describe 'doing x' do
  %i[foo bar foz baz].each do |type|
    it "does a thing \#{type}" do
    end
  end
end

I would imagine that would be very hard to detect (and even harder to autocorrect).

But identical examples or example descriptions within the same iterator can be detected, right? E.g.

describe 'doing x' do
  %i[foo bar].each do |type|
    it "does a thing \#{type}" do
    end

    it "does a thing \#{type}" do
    end
  end
end

@bquorning Yeap, I've added test case with the last examples in same iterator and offenses were detected

Also broken since the last release:

it 'has a long description has a long description' \
   'that continues on the next line' do
  ...
end

it 'has a completely different long description' \
   'with something else on the next line' do
  ...
end
C: RSpec/RepeatedDescription: Don't repeat descriptions within an example group.
        it 'has a long description has a long description ' \ ...
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
C: RSpec/RepeatedDescription: Don't repeat descriptions within an example group.
       it 'has a completely different long description' \ ...
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This should be pretty much covered with dstr:

$ ruby-parse -e "'a' \             
dquote> 'b'"
(dstr
  (str "a")
  (str "b"))

Won't hurt to add an example though.

@jimbali nice case to test, will check it too

Was this page helpful?
0 / 5 - 0 ratings

Related issues

skorks picture skorks  Â·  3Comments

bquorning picture bquorning  Â·  8Comments

bquorning picture bquorning  Â·  4Comments

steiley picture steiley  Â·  3Comments

wscourge picture wscourge  Â·  6Comments