Rubocop-rspec: `RSpec/FilePath` false positive for a file with a shared example group

Created on 19 Aug 2020  ·  8Comments  ·  Source: rubocop-hq/rubocop-rspec

I've update this gem to v1.43.1 and it started catching RSpec/FilePath in my spec/support/shared_examples folder, example:

# spec/support/shared_examples/my_class.rb
shared_examples_for MyClass do
   ...
end

The code above breaks with RSpec/FilePath, it expects my_class_spec.rb instead of just my_class, but the rspec docs and better specs doesn't mention this convention, where does it come from? Looks like a bug to me.

If you have this instead:

# spec/support/shared_examples/my_class.rb
shared_examples_for "my class" do
   ...
end

The code above doesn't expect the file to end with _spec.rb, shouldn't it be consistent?

And If you have a module, like this:

# spec/support/shared_examples/my_module_my_class.rb
shared_examples_for MyModule::MyClass do
   ...
end

It breaks with RSpec/FilePath, as it expects the file to be in spec/support/shared_examples/my_module/my_class_spec.rb, but if you move it there, RSpec fails to run:

NoMethodError:
  undefined method `shared_examples_for' for main:Object

Keeping it short:

  • Where does the _spec.rb file name convention from shared_examples come from? Is this expected or is this a bug?
  • Is the module problem some RSpec config I am missing?
bug

Most helpful comment

doesn't mention this convention, where does it come from? Looks like a bug to me

You're correct, it's a bug. There's no convention that RuboCop RSpec is trying to push through here.

All 8 comments

also doesn't break at cops at all in the code

This is not exactly true to my best knowledge. Can you provide some examples of what is being ignored by cops inside a shared example group with a literal docstring? It makes sense to fix that if it is the case.

also doesn't break at cops at all in the code

This is not exactly true to my best knowledge. Can you provide some examples of what is being ignored by cops inside a shared example group with a literal docstring? It makes sense to fix that if it is the case.

Sorry, I edited the issue, the files where in rubocop_todo.yml, my bad 🤦‍♂️ .

doesn't mention this convention, where does it come from? Looks like a bug to me

You're correct, it's a bug. There's no convention that RuboCop RSpec is trying to push through here.

RSpec fails to run:

NoMethodError:
  undefined method `shared_examples_for' for main:Object

I guess disable_monkey_patching! is turned on in spec_helper.rb, isn't it?
If not, I'd love to know what is causing this. RSpec should handle shared specs disregarding the file name they are in.

RSpec fails to run:

NoMethodError:
  undefined method `shared_examples_for' for main:Object

I guess disable_monkey_patching! is turned on in spec_helper.rb, isn't it?
If not, I'd love to know what is causing this. RSpec should handle shared specs disregarding the file name they are in.

Yes, it is! Changing to RSpec.shared_examples_for solved it, but why does it work when the file is in spec/support/shared_examples and not in spec/support/shared_examples/my_module? 🤔

RSpec.describe "my class" do
   ...
end

The code above doesn't expect the file to end with _spec.rb, shouldn't it be consistent?

It's hard to infer the file name for an arbitrary docstring, so the cop ignores non-constants.

When a constant is quoted, unfortunately, the cops ignores it as well.
You can help RuboCop RSpec by improving the cop's detection just like RSpec/DescribeClass does. It's an easy task, and I'll be happy to help along the way.

RSpec.describe "my class" do
   ...
end

The code above doesn't expect the file to end with _spec.rb, shouldn't it be consistent?

It's hard to infer the file name for an arbitrary docstring, so the cop ignores non-constants.

When a constant is quoted, unfortunately, the cops ignores it as well.
You can help RuboCop RSpec by improving the cop's detection just like RSpec/DescribeClass does. It's an easy task, and I'll be happy to help along the way.

You mean, improve the cop to match the file name when using a string? Like match shared_examples "my clever name" to shared_examples/my_clever_name.rb?

I don't think insisting on my_clever_name.rb is a good idea, but to my point of view

RSpec.describe 'MyNamespace::MyClass' do

should be treated the same way as

RSpec.describe MyNamespace::MyClass do
Was this page helpful?
0 / 5 - 0 ratings

Related issues

pirj picture pirj  ·  4Comments

everaldo picture everaldo  ·  3Comments

sauloperez picture sauloperez  ·  7Comments

bquorning picture bquorning  ·  4Comments

ybiquitous picture ybiquitous  ·  7Comments