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
stub method not found in before(:all) block #92
Comments
I see now that this issue should probably be reported in rspec-core. It looks like run_before_each in lib/rspec/core/example.rb is the "culprit". I'll move this issue to the appropriate repo. |
I don't see the issue in rspec-core yet, so will respond here for now: Stubs in before(:all) are not supported. The fact that it worked for you in rspec-1 was an accident (possibly a bug). The reason is that all stubs and mocks get cleared out after each example, so any stub that is set in before(:all) would work in the first example that happens to run in that group, but not for any others. We've talked about firing off a warning when calling stub or should_receive in a before(:all), but the binding between rspec-core and rspec-mocks combined with the fact that other frameworks would work differently (and some other uglinesses/inconsistencies) led to us not doing that. I'd be happy to entertain a patch that solved that problem, but don't intend to support stubs in before(:all). |
Why stubs in before(:all) is not supported...I think it can be useful in some cases...definitely before(:each) will work as well but why we need to stub one thing before each test? Does it so complicated to support stubs in before(:all)? Sorry if I missed something important |
@khrvi re-read my previous comment: "the reason is ...." |
Makes sense, though I'm curious why you can't just pass in the context in which the stub is activated (suite, all, each) and clear it when the appropriate block is done. The answer that comes to mind is that you run into issues of shared state (if someone unset the stub in an each, it would affect subsequent tests, possibly inconsistently between runs), and it may also not be feasible (can't say without knowing more about the rspec-core/mocks binding issues you alluded to above). Either way, I'd like to add a few keywords in the hope that Google will index them and save some time for the next person who upgrades (in my case, from 2.8 to 2.9, oddly) and encounters the same error I did.
|
@arsduo I've thought of doing that (different scopes) but it would become very confusing for end-users because anything that is stubbed in more than one scope would get reset at the end of the smallest scope. e.g. before(:all) do
Thing.stub(:find => double('thing'))
end
it "does something in some condition" do
Thing.stub(:something_else => double('thing'))
# when this example ends, Thing is reset, removing the stubs of both 'find' and 'something_else'.
end |
Would it make sense to create a stub method, available in before(:all), that simply stores a set of stubs to run before(:each) ? I would think that would have the intended effect - although in that case, we might as well just encourage putting it in before(:each). So, long story short, I guess agree with the idea of just triggering a warning when the stub method is called in before(:all) if that can be made possible. That seems like the right answer. |
Absolutely not! That would become very confusing if we ever decide to support stubs in before(:all) in the future. If anything we could add an implementation that raises an error explaining what is/is not supported. |
The fact that this didn't raise anything but a NoMethodError sent me on hours of chasing through documentation and discussions trying to figure out what's wrong with rspec. I have a suite that really only uses before(:all) since the setup is so very time consuming, so my experience was that the stub method just didn't exist no matter what I did. Can rspec at least raise when this is used in a context that doesn't support it. I just spent my day on this before I figured out it was the before(:all) context causing it. |
@martinemde -- thanks for reminding us about this issue. I agree we should improve this. I've opened #240 to track that work. |
@myronmarston Thank you! That'll help save some others from table to head collisions. |
summary for 2017 visitors: use |
Migrating from Rais 2.3 to Rails 3 and Rspec 1 to Rspec 2. Previously passing test with stub in before(:all) block now fails. I added the scenario below to the rspec-mock feature suite and it fails.
The text was updated successfully, but these errors were encountered: