-
-
Notifications
You must be signed in to change notification settings - Fork 762
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
Comments
Can you post an example demonstrating what you mean? |
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. |
Actually, I think the warning is the right behavior. Consider that every time 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 |
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. |
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. |
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. |
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:
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 |
@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? |
I also have the same question about shared_examples in a shared_context as @adamgotterer, how would you recommend I organize them? |
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. |
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 |
Just in case someone googles and lands here. If putting your file with shared examples into
Make sure your filename does not end with |
So we don't load it twice. see: rspec/rspec-core#828
So we don't load it twice. see: rspec/rspec-core#828
Someone give Mr. @uzbekjon a star. Thank you, random citizen :D |
I also have googled and found this useful. Thanks @uzbekjon! |
Thanks for the great tip @uzbekjon! |
It works for me. Thanks @uzbekjon . |
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.
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.
Thank you, @uzbekjon! |
A co-worker accidently called
shared_examples_for
within anothershared_examples_for
block. This lead to this confusing warning: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.
The text was updated successfully, but these errors were encountered: