Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading warning when calling shared_examples_for within another shared_examples_for block #828

Closed
jmuheim opened this issue Mar 11, 2013 · 18 comments

Comments

@jmuheim
Copy link

jmuheim commented Mar 11, 2013

A co-worker accidently called shared_examples_for within another shared_examples_for block. This lead to this confusing warning:

WARNING: Shared example group 'all filterable and searchable lists' has been previously defined at:
  /Users/josh/Documents/Work/Sientia/iq/spec/support/shared_examples/activity_list_examples.rb:225
...and you are now defining it at:
  /Users/josh/Documents/Work/Sientia/iq/spec/support/shared_examples/activity_list_examples.rb:225
The new definition will overwrite the original one.

This warning is printed out for every spec that calls the shared example (except the first one).

Maybe you want to add some mechanism that prevents this problem.

Thank you.

@myronmarston
Copy link
Member

Can you post an example demonstrating what you mean?

@jmuheim
Copy link
Author

jmuheim commented Mar 11, 2013

shared_examples_for 'foo' do
  shared_examples_for 'bar' do
    # ...
  end
end

This should result in the mentioned warning. We had this problem when a git diff was merged wrong, so the 2nd shared example ended up within the 1st.

@myronmarston
Copy link
Member

Actually, I think the warning is the right behavior. Consider that every time shared_examples_for 'bar' runs, it defines a global example group named 'bar'. We used to raise an error in this situation, and I think the warning is an improvement. You might say that we should be able to see that it's being redefined at the same point in code it was originally defined (and thus we can safely ignore it), but consider that shared example groups take arguments, so you could wind up with:

shared_examples_for 'foo' do |arg|
  shared_examples_for 'bar' do
    if arg
      # define some examples
    else
      # define some other examples
    end
  end
end

it_behaves_like 'foo', true
it_behaves_like 'foo', false

Here, the bar shared example group gets defined twice, and each time it is different. Thus, the second time shared_examples_for 'bar' runs, it is in fact overwriting the original definition of the bar shared example group, and even though it might be counterintuitive, the warning is actually accurate.

@jmuheim
Copy link
Author

jmuheim commented Mar 11, 2013

This absolutey makes sense. Still, it cost me an hour to find the problem, and maybe you could point into the right direction when the line numbers of the "redefined" example is the same like the original example's?

Just an idea, maybe there are too many edge cases you guys would have to think about if you would care about this special case.

@myronmarston
Copy link
Member

Still, it cost me an hour to find the problem, and maybe you could point into the right direction when the line numbers of the "redefined" example is the same like the original example's?

I can't think of anything else to include in the warning message that would explain it any more clearly without being excessively verbose (but then again, it already made perfect sense to me -- it's not clear to me what was confusing or misleading to you about it).

If you can think of a better wording for the warning message that would help prevent confusion, feel free to open a pull request.

@adamgotterer
Copy link

I'm running into this issue with a shared_context that has a bunch of shared_examples in it. What is the suggested best practice for organizing these? I'd stick them in a module or something but I have some higher level matchers and let groups that are shared amongst the shared_examples.

@pjammer
Copy link

pjammer commented Aug 28, 2013

I see this warning too when running the tests, and our shared examples are not in a block of shared examples. Just in spec_helper.rb:

WARNING: Shared example group 'global session' has been previously defined at:
  /Users/pjammer/app/spec/spec_helper.rb:162
...and you are now defining it at:
  /Users/pjammer/app/spec/spec_helper.rb:162
The new definition will overwrite the original one.

fwiw here is the code:

shared_examples "active" do |klass, url|
  it "filters by active" do
    klass.should_receive(:active).once
    get url + "?active"
  end
end

@myronmarston
Copy link
Member

@pjammer -- your code snippet clearly isn't what is triggering that warning. The warning mentions it's the "global session" shared example group. Can you paste your entire spec helper file?

@kyledemeule
Copy link

I also have the same question about shared_examples in a shared_context as @adamgotterer, how would you recommend I organize them?

@JonRowe
Copy link
Member

JonRowe commented Sep 12, 2013

Place them in a file where they are only loaded once. If you need to reuse them across multiple spec files I suggest you put them in a support file. e.g. spec/support/shared_examples.rb then require that file where you need them. Ruby won't require a file twice.

@JonRowe
Copy link
Member

JonRowe commented Sep 12, 2013

BTW this isn't a problem we can fix, because even if the file and number is the same it's no guarentee the contents of the shared_examples are the same due to the way Ruby meta programming works. (You could easily be evaling string or other code contents).

Also in RSpec 3 shared_examples will become context aware, so that you'll need them either top level or nested appropriately at that point anyhow.

@uzbekjon
Copy link

Just in case someone googles and lands here. If putting your file with shared examples into support folder has not fixed the following error:

WARNING: Shared example group 'your shared example' has been previously defined at:
 /spec/support/your_shared_example_file_spec.rb:1
...and you are now defining it at:
  /spec/support/your_shared_example_file_spec.rb:1
The new definition will overwrite the original one.

Make sure your filename does not end with _spec.rb.

alexwelch added a commit to CenturyLinkLabs/panamax-api that referenced this issue Apr 14, 2014
So we don't load it twice.
see: rspec/rspec-core#828
alexwelch added a commit to CenturyLinkLabs/panamax-api that referenced this issue Apr 14, 2014
So we don't load it twice.
see: rspec/rspec-core#828
@jaimerson
Copy link

Someone give Mr. @uzbekjon a star. Thank you, random citizen :D

@grossadamm
Copy link

I also have googled and found this useful. Thanks @uzbekjon!

@PotHix
Copy link

PotHix commented Jul 25, 2014

Thanks for the great tip @uzbekjon!

@jechol
Copy link

jechol commented Sep 24, 2014

It works for me. Thanks @uzbekjon .

@freemanoid
Copy link

@uzbekjon 👍

pozorvlak pushed a commit to pozorvlak/growstuff that referenced this issue Nov 2, 2014
We were getting the warning

```
WARNING: Shared example group 'crop suggest' has been previously defined
at:
  /Users/miles/src/growstuff/spec/features/shared_examples/crop_suggest_spec.rb:3
...and you are now defining it at:
  /Users/miles/src/growstuff/spec/features/shared_examples/crop_suggest_spec.rb:3
The new definition will overwrite the original one.
```

Following the suggestion at
rspec/rspec-core#828 (comment),
I've renamed crop_suggest_spec.rb to crop_suggest.rb, which made the
error going away without reducing the number of tests run. RSpec must
have thought it was a spec file and loading it directly, then loading it
again when it was first required by an actual spec file.
pozorvlak pushed a commit to pozorvlak/growstuff that referenced this issue Nov 2, 2014
We were getting the warning

```
WARNING: Shared example group 'crop suggest' has been previously defined
at:
  /Users/miles/src/growstuff/spec/features/shared_examples/crop_suggest_spec.rb:3
...and you are now defining it at:
  /Users/miles/src/growstuff/spec/features/shared_examples/crop_suggest_spec.rb:3
The new definition will overwrite the original one.
```

Following the suggestion at
rspec/rspec-core#828 (comment),
I've renamed crop_suggest_spec.rb to crop_suggest.rb, which made the
error go away without reducing the number of tests run. RSpec must
have thought it was a spec file and loaded it directly, then loaded it
again when it was first required by an actual spec file.
@SalvatoreT
Copy link

Thank you, @uzbekjon!

@rspec rspec locked and limited conversation to collaborators Nov 11, 2014
@rspec rspec locked and limited conversation to collaborators Nov 11, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests