rspec / rspec-core

RSpec runner and formatters
http://rspec.info
MIT License
1.22k stars 763 forks source link

Strange warning about shared example groups #2277

Closed devonestes closed 8 years ago

devonestes commented 8 years ago

Hey y'all,

So I just noticed today that I've bene getting a strange warning about a shared example group. See below:

WARNING: Shared example group 'a flagger_or_tagger' has been previously defined at:
  /Users/devoncestes/esh/IRT/spec/services/shared_examples/flagger_or_tagger_spec.rb:10
...and you are now defining it at:
  /Users/devoncestes/esh/IRT/spec/services/shared_examples/flagger_or_tagger_spec.rb:10
The new definition will overwrite the original one.

I'm not really sure how it's being defined twice in the same place, but that's what this warning is telling me! This might not even be the cause of any real problems, but it seemed strange enough for me to bring up. If there's anything else I can provide to help, just let me know!

myronmarston commented 8 years ago

The problem is that your shared group is defined in a file named *_spec.rb, which matches the pattern of files RSpec looks to load automatically. Then you are also requiring it somewhere so it is loaded twice.

Just rename your file to something not ending in _spec.rb so RSpec does not load it automatically.

devonestes commented 8 years ago

Oh, great! Thanks for the tip, @myronmarston!

devonestes commented 8 years ago

On second thought, do you think this warrants some sort of special message to the user with helpful information so they can fix cases like this? I'm not sure this happens all that frequently, but I just wanted to toss the idea out there.

myronmarston commented 8 years ago

That's not a bad idea. The logic could see that the file matches the configured pattern used to find spec files and mention that it should be renamed to not match the pattern if it's a support file intended to be loaded and used by other spec file. If it does not match the pattern then it should not mention that.

Want to take a stab at it?

devonestes commented 8 years ago

Sure thing - I'll give it a whirl and see how far I get!

Victorcorcos commented 4 years ago

Thank you 🗡️ I just opened a PR on my Project to fix this really annoying warning issue!

Command used 🎯

for file in *.rb; do mv "$file" "${file/_spec.rb/.rb}"; done

Description

All of our Shared Examples files were loading two times, because all of them ended with the _spec suffix!! => https://github.com/rspec/rspec-core/issues/2277#issuecomment-230069423 That's why we had so many warning messages like this one!

Screen Shot 2020-06-11 at 20 25 49

pirj commented 4 years ago

Another option would be not to require files containing shared examples manually. Choose between those two options wisely, if your target is to avoid time-consuming constant autoloading, then manually requiring shared examples is something you should really consider doing.