[Double-ruby-users] Mocking a method of any instance of a class
Dmitry Ratnikov
ratnikov at google.com
Wed Jul 14 14:07:00 EDT 2010
Unfortunately that's too little to debug of what's going on. Perhaps you
can present a git (gitst.github.com)?
Also, it would be great if you didn't have any external dependencies (e.g.
AR::Base) in your sample.
Dmitry
2010/7/13 Jonathon Horsman <jonathon at arctickiwi.com>
> Thanks for your help, that does indeed work.
>
> However now I have a new problem:
>
> stub.instance_of(Search).x { 3 }
>
> puts Search.new.x # displays 3
> puts Search.first.x # gives me an error: undefined method `x' for #<Search:0xb5fabaa8>
>
>
>
> On 12 July 2010 19:01, Dmitry Ratnikov <ratnikov at google.com> wrote:
>
>> This worked for me:
>>
>> class FooTest < Test::Unit::TestCase
>> class CallList
>> def mocked
>> 3
>> end
>> end
>>
>> def test_mock
>> mock.instance_of(CallList).mocked { 2 }
>>
>> x = CallList.new
>>
>> assert_equal 2, x.mocked
>> end
>> end
>>
>> Perhaps you need to mock it out first and only then instantiate the
>> object.
>>
>> -- D
>>
>> 2010/7/12 Jonathon Horsman <jonathon at arctickiwi.com>
>>
>>> Hello
>>>
>>> I'm trying to use Ruby Double to mock out the return value of a method of
>>> any instance of a particular class:
>>>
>>> class CallList
>>> def mocked
>>> 3
>>> end
>>> end
>>>
>>> and the test:
>>>
>>> describe CallList do
>>> it "should test x" do
>>> x = CallList.new
>>> mock.instance_of(CallList).mocked {2}
>>> x.mocked.should == 2
>>> end
>>> end
>>>
>>> the test fails and I get 3. Am I misunderstanding instance_of?
>>>
>>> Thanks
>>>
>>> --
>>> Jonathon Horsman
>>> Director - Arctic Kiwi
>>> Mobile: 079 42366038
>>> www.arctickiwi.com
>>>
>>> _______________________________________________
>>> Double-ruby-users mailing list
>>> Double-ruby-users at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/double-ruby-users
>>>
>>>
>>
>
>
> --
> Jonathon Horsman
> Director - Arctic Kiwi
> Mobile: 079 42366038
> www.arctickiwi.com
>
> _______________________________________________
> Double-ruby-users mailing list
> Double-ruby-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/double-ruby-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/double-ruby-users/attachments/20100714/d4f17deb/attachment-0001.html>
More information about the Double-ruby-users
mailing list