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

David Chelimsky dchelimsky at gmail.com
Mon Jul 5 16:26:18 EDT 2010

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

> 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 => "sally at etestflight.com"})
>       User.should_receive(:create).with({:email => "joe at etestflight.com"})
>       User.stub!(:find_by_email).with("sally at etestflight.com") { nil }
>       User.stub!(:find_by_email).with("joe at etestflight.com") { nil }
>       User.stub!(:find_by_email).with("bill at etestflight.com") { User.new }
>       begin
>         post :route, :id => "37", :email_addresses => "sally at etestflight.com, joe at etestflight.com, bill at etestflight.com"
>       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).

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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100705/994e6e07/attachment-0001.html>

More information about the rspec-users mailing list