# \[Rails\] Stubbed static methods are bleeding over into other specs and making them fail

**URL:** <https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032>\
**Category:** RSpec\
**Created:** [July 3, 2010, 2:28am UTC](https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032 "2010-07-03T02:28:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [July 3, 2010, 2:28am UTC](https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032/1 "2010-07-03T02:28:57Z")

</div>

Hi all,

I’m having a problem where stubbed static methods aren’t properly  
becoming  
“unstubbed”. I have tried manually unstubbing them, but it still  
doesn’t  
work.

Spec is as follows:

```
it "creates user accounts for unknown users" do
  Notification.stub(:deliver_routed)
  User.should_receive(:create).with({:email => 

```

“removed\_email\_address@domain.invalid”})  
User.should\_receive(:create).with({:email =\>  
“removed\_email\_address@domain.invalid”})  
User.stub!(:find\_by\_email).with(“removed\_email\_address@domain.invalid”) { nil }  
User.stub!(:find\_by\_email).with(“removed\_email\_address@domain.invalid”) { nil }  
User.stub!(:find\_by\_email).with(“removed\_email\_address@domain.invalid”) { User.new  
}

```
  begin
    post :route, :id => "37", :email_addresses => 

```

“removed\_email\_address@domain.invalid,  
removed\_email\_address@domain.invalid, removed\_email\_address@domain.invalid”  
ensure  
User.unstub! :find\_by\_email  
#User.unstub! :method\_missing  
User.rspec\_reset  
end  
end

It will pass, but then another spec fails with: “undefined method  
`find\_by\_email’ for Object:Class”. The other spec passes when I create  
a  
database User and rely on the real definition of User.find\_by\_email  
_(defined  
by method\_missing in MongoMapper’s Document module)_ in this spec  
instead of  
stubbing out the calls to find\_by\_email. As you can see, I tried  
calling  
“unstub!” on find\_by\_email, and calling rspec\_reset, and it doesn’t make  
any  
difference (other spec still fails). I even tried un-stubbing  
method\_missing (since that’s what defines the real find\_by\_email) and it  
complains that that was never stubbed.

I am running Rails 3 beta 4 with RSpec and rspec-rails beta 13 (tried  
pointing Gemfile to the GIT repository and did a ‘bundle install’, but  
it  
still installed beta 13 instead of 15 for some reason). Here is a list  
of  
the gems I have installed (via bundle show)

Gems included by the bundle:

- abstract (1.0.0)
- actionmailer (3.0.0.beta4)
- actionpack (3.0.0.beta4)
- activemodel (3.0.0.beta4)
- activerecord (3.0.0.beta4)
- activeresource (3.0.0.beta4)
- activesupport (3.0.0.beta4)
- arel (0.4.0)
- bcrypt-ruby (2.1.2)
- bson (1.0.3)
- bson\_ext (1.0.1)
- builder (2.1.2)
- bundler (1.0.0.beta.2)
- capistrano (2.5.19)
- capybara (0.3.8 35e81a4)
- cucumber (0.8.3)
- cucumber-rails (0.3.2 b75110d)
- culerity (0.2.10)
- database\_cleaner (0.5.2 7ea99d7)
- devise (1.1.rc1 88ab2fe)
- diff-lcs (1.1.2)
- erubis (2.6.6)
- factory\_girl (1.3.0)
- factory\_girl\_rails (1.0)
- faker (0.3.1)
- ffi (0.6.3)
- gherkin (2.0.2)
- highline (1.5.2)
- i18n (0.4.1)
- jnunemaker-validatable (1.8.4)
- joint (0.3.2 11a094d)
- json\_pure (1.4.3)
- mail (2.2.5)
- mime-types (1.16)
- mongo (1.0.3)
- mongo\_mapper (0.8.2)
- net-scp (1.0.2)
- net-sftp (2.0.4)
- net-ssh (2.0.23)
- net-ssh-gateway (1.0.1)
- nokogiri (1.4.2.1)
- plucky (0.3.2)
- polyglot (0.3.1)
- rack (1.2.1)
- rack-test (0.5.4)
- rails (3.0.0.beta4 f8720a0)
- railties (3.0.0.beta4)
- rake (0.8.7)
- rspec (2.0.0.beta.13)
- rspec-core (2.0.0.beta.13)
- rspec-expectations (2.0.0.beta.13)
- rspec-mocks (2.0.0.beta.13)
- rspec-rails (2.0.0.beta.13)
- selenium-webdriver (0.0.24)
- term-ansicolor (1.0.5)
- thor (0.13.7)
- treetop (1.4.8)
- trollop (1.16.2)
- tzinfo (0.3.22)
- wand (0.2.1)
- warden (0.10.7)
- webrat (0.7.1)

Thank you!  
Pat

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [July 5, 2010, 10:42pm UTC](https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032/2 "2010-07-05T22:42:33Z")

</div>

On Jul 2, 2010, at 7:25 PM, Patrick G. wrote:

> ```
> User.stub!(:find_by_email).with("removed_email_address@domain.invalid") { nil }
> end
> 
> ```
> 
> It will pass, but then another spec fails with: “undefined method `find\_by\_email’ for Object:Class”. The other spec passes when I create a database User and rely on the real definition of User.find\_by\_email (defined by method\_missing in MongoMapper’s Document module) in this spec instead of stubbing out the calls to find\_by\_email. As you can see, I tried calling “unstub!” on find\_by\_email, and calling rspec\_reset, and it doesn’t make any difference (other spec still fails). I even tried un-stubbing method\_missing (since that’s what defines the real find\_by\_email) and it complains that that was never stubbed.
> 
> I am running Rails 3 beta 4 with RSpec and rspec-rails beta 13 (tried pointing Gemfile to the GIT repository and did a ‘bundle install’, but it still installed beta 13 instead of 15 for some reason).

I just pushed what I believe to be a fix for this - please try pointing  
Gemfile to the git repos again. It should work if you do this with all  
of them:

gem “rspec-rails”, :git =\>  
“git://github.com/rspec/rspec-rails.git”  
gem “rspec”, :git =\> “git://github.com/rspec/rspec.git”  
gem “rspec-core”, :git =\>  
“git://github.com/rspec/rspec-core.git”  
gem “rspec-expectations”, :git =\>  
“git://github.com/rspec/rspec-expectations.git”  
gem “rspec-mocks”, :git =\>  
“git://github.com/rspec/rspec-mocks.git”

Let me know if it works out.

Thx,  
David

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [July 10, 2010, 3:02am UTC](https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032/3 "2010-07-10T03:02:43Z")

</div>

On Mon, Jul 5, 2010 at 1:26 PM, David C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> Let me know if it works out.

Hi David,

I just got a chance to try this, and ran into more problems. Prior to  
trying this, we were running RSpec2 beta 13. When I changed our Gemfile  
to  
depend on ‘edge’ RSpec et al (as you suggested above), I get a very  
strange  
error when I run rake (after doing a ‘bundle install’). The same error  
occurs after I tell it specifically to use beta 16 (rather than ‘edge’).  
It  
seems to somehow not be able to find the bundler gem when it runs the  
main  
bundler script from within rake:

C:/Ruby187/lib/ruby/site\_ruby/1.8/rubygems.rb:779:in  
`report_activate_error': Could not find RubyGem bundler (>= 0) (Gem::LoadError) from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:214:in `activate’  
from C:/Ruby187/lib/ruby/site\_ruby/1.8/rubygems.rb:1082:in `gem’  
from C:/Ruby187/bin/bundle:18  
rake aborted!  
bundle exec C:/Ruby187/bin/ruby.exe -Ilib -Ispec  
“./spec/controllers/discrepancies\_controller\_spec.rb” _(…spec names  
omitted..)_ failed

When I downgrade back to beta 13, everything works again. I can’t seem  
to  
figure out why it wouldn’t be able to find the bundler gem (since  
obviously  
bundler is installed), so I cannot say whether or not your fix for the  
stubbing issue solved my problem. The stack trace (resulting from  
running  
rake with “–trace”) and my current list of installed gems (via “bundle  
list”) is here: [RSpec beta 16 installed gems · GitHub](http://gist.github.com/470278) (sorry for the line  
breaks)

Thanks for your help!  
Pat Gannon

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [July 10, 2010, 10:04pm UTC](https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032/4 "2010-07-10T22:04:01Z")

</div>

On Jul 9, 2010, at 7:37 PM, Patrick G. wrote:

> Let me know if it works out.  
> rake aborted!  
> bundle exec C:/Ruby187/bin/ruby.exe -Ilib -Ispec “./spec/controllers/discrepancies\_controller\_spec.rb” (…spec names omitted..) failed
> 
> When I downgrade back to beta 13, everything works again. I can’t seem to figure out why it wouldn’t be able to find the bundler gem (since obviously bundler is installed), so I cannot say whether or not your fix for the stubbing issue solved my problem. The stack trace (resulting from running rake with “–trace”) and my current list of installed gems (via “bundle list”) is here: [RSpec beta 16 installed gems · GitHub](http://gist.github.com/470278) (sorry for the line breaks)

bundler-1.0.0.beta.4 should fix this. Please give it a shot.

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [July 24, 2010, 10:21am UTC](https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032/5 "2010-07-24T10:21:20Z")

</div>

On Fri, Jul 23, 2010 at 7:29 PM, Patrick G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ```
> > from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems.rb:214:in
> > 
> > ```
> > 
> > stubbing issue solved my problem. The stack trace (resulting from running  
> > require each of the railties other than ActiveRecord in application.rb,  
> > Pat  
> > OK - I believe I’ve fixed the ActiveRecord problem. Please try “bundle  
> > install” again (make sure you get rspec-rails \>=  
> > 83f2a44bfd31e7133248bba67bfe3ad89dc59b27.

HTH,  
David

---

<div class="post-metadata">

**Author:** ![pgundlach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b9e5f3/32.png) [@pgundlach](https://www.ruby-forum.com/u/pgundlach)\
**Post date:** [July 24, 2010, 3:03am UTC](https://www.ruby-forum.com/t/rails-stubbed-static-methods-are-bleeding-over-into-other-specs-and-making-them-fail/191032/6 "2010-07-24T03:03:35Z")

</div>

> `report\_activate\_error’: Could not find RubyGem bundler (\>= 0)  
> When I downgrade back to beta 13, everything works again. I can’t seem to  
> figure out why it wouldn’t be able to find the bundler gem (since obviously  
> bundler is installed), so I cannot say whether or not your fix for the  
> stubbing issue solved my problem. The stack trace (resulting from running  
> rake with “–trace”) and my current list of installed gems (via “bundle  
> list”) is here: [RSpec beta 16 installed gems · GitHub](http://gist.github.com/470278) (sorry for the line breaks)
> 
> bundler-1.0.0.beta.4 should fix this. Please give it a shot.

Ok, I upgraded to bundler 1.0 beta 9 and configured Gemfile to use edge  
RSpec et al (as recommended above). After doing a ‘bundle install’, our  
tests failed (without trying to re-enable any of the mocking  
functionality  
originally mentioned). It appears that now RSpec is trying to reference  
ActiveRecord, even though we’re not using ActiveRecord. (We  
specifically  
require each of the railties other than ActiveRecord in application.rb,  
rather than requiring rails/all.) The error that rake produces is:  
Don’t  
know how to build task ‘db:test:prepare’. I then went ahead and defined  
an  
empty rake task called db:test:prepare, and then got another error  
relating  
to ActiveRecord: uninitialized constant  
RSpec::Rails::FixtureSupport::ActiveRecord (NameError). Stack trace is  
here: [Test failure · GitHub](https://gist.github.com/acbaf0a384b301013140) (sorry for the  
formatting

- copied/pasted from a DOS window). I then tried to use RSpec 2.0  
beta.16  
(rather than edge) and it skipped straight to the aforementioned error  
(even  
when db:test:prepare wasn’t defined, it didn’t complain about that).

Thanks,  
Pat
