On Apr 25, 2011, at 11:30 PM, Rodrigo Rosenfeld Rosas wrote:
> Em 25-04-2011 14:58, Matthew Van Horn escreveu:
>>
>>
>> On Apr 25, 2011, at 1:36 PM, Matthew Van Horn wrote:
>>
>>> I've run into some strange behavior in porting my specs from rspec to
>>> rspec2.
>>> I am wondering if I am doing something wrong, or if I've misunderstood
>>> something, or if this is some kind of bug.
>>>
>>> Look at the specs below: a
>>> Both examples will pass if run singly.
>>> The second one will fail if I run both examples.
>>>
>>> Can someone tell me why the second one fails the way it does?
>>>
>>> It has to do with the conditional assignment of @my_foo, but I'm not sure
>>> why @stupid_mock is getting disconnected.
>>> My thinking is that the class Bar is returning the same object in both
>>> tests, and that the stub call in the second test is being sent to a
>>> different object.
>>>
>>> Still, it did not behave this way in the last version, so I don't know what
>>> I'm missing.
>>>
>>> require 'spec_helper'
>>>
>>> class Foo
>>> end
>>>
>>> class Bar
>>> def self.my_foo
>>> @my_foo ||= Foo.new
>>> end
>>> def self.perform
>>> my_foo.do_something
>>> end
>>> end
>>>
>>> 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
>>
>> btw - I just realized, that should be: "describe Bar do"
>> and, I can solve the problem by doing: Bar.stub(:my_foo => @stupid_mock)
>> instead of Foo.stub(:new => @stupid_mock) but I really don't like stubbing
>> methods on the class I am testing.
>>
>
> The problem is that "new" is not a regular method on Ruby. I don't mind
> mocking some methods of the class being tested in some cases.
It doesn't have to do with the "new" method - if I did:
class Bar def self.my_foo
@my_foo ||= Foo.make_one_of_these
end
...
end
and
Foo.stub(:make_one_of_these => @stupid_mock)
It still would fail.
It is more of a pass-by-value/pass-by-reference issue. Because of the
conditional assignment, the variable @stupid_mock in the failing example seems
to be pointing to a different object than the one in the passing example, even
though under the old rspec, they seemed to be the same.
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users