Forum: Ruby How to create a MiniTest::Mock object that expects to_s

Posted by unknown (Guest)
on 2012-10-12 18:36
(Received via mailing list)
I suspect it is not possible, but maybe I'm lucky:
how can I create a mock object with MiniTest that expects a
call to the to_s method?

1.9.3-p194 :001 > require 'minitest/mock'
...
1.9.3-p194 :002 > obj = MiniTest::Mock.new
...
1.9.3-p194 :003 > obj.expect(:size, 5)
...
1.9.3-p194 :004 > obj.expect(:to_s, 'an object')
...
1.9.3-p194 :005 > obj.size
  => 5
1.9.3-p194 :006 > obj.to_s
  => "#<MiniTest::Mock:0x82eea98>"
Posted by unknown (Guest)
on 2012-10-14 16:56
(Received via mailing list)
Am 12.10.2012 18:35, schrieb sto.mar@web.de:
> 1.9.3-p194 :004 > obj.expect(:to_s, 'an object')
> ...
> 1.9.3-p194 :005 > obj.size
>   => 5
> 1.9.3-p194 :006 > obj.to_s
>   => "#<MiniTest::Mock:0x82eea98>"

My workaround for the moment is using a singleton method:

1.9.3-p194 :001 > require 'minitest/mock'
1.9.3-p194 :002 > obj = MiniTest::Mock.new
1.9.3-p194 :003 > obj.expect(:size, 5)
1.9.3-p194 :004 > def obj.to_s; 'an object'; end
1.9.3-p194 :005 > obj.size
   => 5
1.9.3-p194 :006 > obj.to_s
   => "an object"
Posted by unknown (Guest)
on 2012-10-19 17:09
(Received via mailing list)
Am 14.10.2012 16:56, schrieb sto.mar@web.de:
>> ...
> 1.9.3-p194 :002 > obj = MiniTest::Mock.new
> 1.9.3-p194 :003 > obj.expect(:size, 5)
> 1.9.3-p194 :004 > def obj.to_s; 'an object'; end
> 1.9.3-p194 :005 > obj.size
>    => 5
> 1.9.3-p194 :006 > obj.to_s
>    => "an object"

Am still interested in how to do this properly. Thanks.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.