Ok, now I understand what is your issue. class Fooend class Bar def self.my_foo @my_foo ||= Foo.new end def self.perform my_foo.do_something endend describe Foo do before(:each) do @stupid_mock = double('wtf') Foo.stub(:new => @stupid_mock) end it "passes here" do @stupid_mock.should_receive(:do_something).and_return('value') Bar.perform end it "fails here" do @stupid_mock.stub(:do_something => 'value') Bar.perform # double "wtf" received unexpected message :do_something with (no args) end end
The first time Bar.foo is being called, it caches Foo.new. Since you stubbed Foo.new to return a mock before running Bar.foo for the first time, this mock is cached inside Bar, so any subsequent call will return the first created mock object. This obviously won't happen if you run only the other spec and that is why it passes in this case. This is not about passing by reference, but it is about caching Bar.my_foo. The only way I can think of RSpec not being affected by this is to reloading all classes before each example, which would be both complicate and costly. You'll probably have to rethink your testing strategies, avoiding caching class methods or not mocking something that could affect them, or initializing them first before running the specs... Best regards, Rodrigo.
_______________________________________________ rspec-users mailing list rspec-users@rubyforge.org http://rubyforge.org/mailman/listinfo/rspec-users