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

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 => 

[email protected]”})
User.should_receive(:create).with({:email =>
[email protected]”})
User.stub!(:find_by_email).with(“[email protected]”) { nil }
User.stub!(:find_by_email).with(“[email protected]”) { nil }
User.stub!(:find_by_email).with(“[email protected]”) { User.new
}

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

[email protected],
[email protected], [email protected]
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

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

  User.stub!(:find_by_email).with("[email protected]") { 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

On Mon, Jul 5, 2010 at 1:26 PM, David C.
[email protected]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 (sorry for the line
breaks)

Thanks for your help!
Pat Gannon

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 (sorry for the line breaks)

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

On Fri, Jul 23, 2010 at 7:29 PM, Patrick G. [email protected]
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

`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 (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 (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