Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec shared definitions loaded twice

I haven't been able to pinpoint the cause of this, but I'm getting deprecation warnings of shared specs being loaded more than once in a Rails project. Here's how they are defined:

# spec/support/shared/authenticated_endpoints_spec.rb

RSpec.shared_examples "an authenticated endpoint" do
  it_behaves_like "an authenticated show endpoint"
  it_behaves_like "an authenticated index endpoint"
end

RSpec.shared_examples "an authenticated show endpoint" do
# omitted
end
RSpec.shared_examples "an authenticated index endpoint" do
# omitted
end

My spec_helper looks like this:

RSpec.configure do |config|
  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
end

This is the Deprecation warning I've been getting:

WARNING: Shared example group 'an authenticated endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
The new definition will overwrite the original one.
WARNING: Shared example group 'an authenticated show endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:6
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:6
The new definition will overwrite the original one.
WARNING: Shared example group 'an authenticated index endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:36
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:36
The new definition will overwrite the original one.

I am not requiring these shared specs anywhere else (that I can find, anyway), in the test suite. I looked through rspec-core and there's a lot of meta programming going on that I don't quite understand.

Anyone have tips on how to debug this?

like image 880
mehulkar Avatar asked Apr 07 '26 11:04

mehulkar


2 Answers

I believe the reason is described here. Since your file names are ended with "_spec.rb", they get autoloaded as examples by RSpec. What I did and what helped, is renaming the files, containing shared examples, removing the "_spec" portion.

like image 129
The Ancient Avatar answered Apr 08 '26 23:04

The Ancient


There's a closed issue on rspec-rails for this. Basically, RSpec uses a pattern matcher (spec/**/*_spec.rb) to find spec files and auto-loads them. But your spec helper auto-loads all files in the spec/support/ subdirectory. So your spec/support/shared/authenticated_endpoints_spec.rb file is being loaded twice.

I'd suggest moving the spec/support/shared/ subdirectory to spec/shared.

like image 30
Johnson Avatar answered Apr 09 '26 00:04

Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!