[TIP] mock.ANY not actually ANY?

Michael Foord michael at voidspace.org.uk
Mon Jan 9 16:26:04 PST 2012


On 10/01/2012 00:23, Alex Gaynor wrote:
>
>
> On Mon, Jan 9, 2012 at 6:16 PM, Michael Foord 
> <michael at voidspace.org.uk <mailto:michael at voidspace.org.uk>> wrote:
>
>     On 09/01/2012 22:41, Tom Davis wrote:
>>     I have a mock that is using assert_called_with() and mock.ANY thus:
>>
>>     m = mock.Mock()
>>     do_stuff(m)
>>     m.assert_called_with(now=mock.ANY, foo='bar')
>>
>>     Unfortunately, this results in:
>>
>>     AssertionError: Expected call: mock(now=<ANY>, foo='bar')
>>     Actual call: mock(now=datetime.datetime(2012, 1, 9, 22, 36, 41,
>>     264838), foo='bar')
>>
>>     I could have sworn that ANY used to work perfectly fine when used
>>     in this way. Is this a datetime thing or...?
>>
>>     Using mock==dev
>
>     Hmmm... dammit. I see the same thing, but I think I see the cause
>     of the problem:
>
>     >>> from mock import Mock, ANY
>     >>> from datetime import datetime
>     >>> d = datetime.now()
>     >>> m = Mock()
>     >>> m(d)
>     <Mock name='mock()' id='4312271632'>
>     >>> m.assert_called_with(ANY)
>     Traceback (most recent call last):
>      ...
>     AssertionError: Expected call: mock(<ANY>)
>     Actual call: mock(datetime.datetime(2012, 1, 10, 0, 13, 52, 593795))
>     >>> ANY == d
>     True
>     >>> d == ANY
>     False
>
>     It only happens with certain types:
>
>     >>> m(3)
>     <Mock name='mock()' id='4312271632'>
>     >>> m.assert_called_with(ANY)
>     >>>
>
>     I'll try and fix this, thanks for the report.
>
>     All the best,
>
>     Michael Foord
>>
>>
>>     _______________________________________________
>>     testing-in-python mailing list
>>     testing-in-python at lists.idyll.org  <mailto:testing-in-python at lists.idyll.org>
>>     http://lists.idyll.org/listinfo/testing-in-python
>
>
>     -- 
>     http://www.voidspace.org.uk/
>
>     May you do good and not evil
>     May you find forgiveness for yourself and forgive others
>     May you share freely, never taking more than you give.
>     -- the sqlite blessinghttp://www.sqlite.org/different.html
>
>
>     _______________________________________________
>     testing-in-python mailing list
>     testing-in-python at lists.idyll.org
>     <mailto:testing-in-python at lists.idyll.org>
>     http://lists.idyll.org/listinfo/testing-in-python
>
>
> The issue is, timedelta (in it's infinite wisdom), does not return 
> NotImplemented from __eq__ on non-timedelta values, so ANY.__eq__ 
> isn't invoked.

Yeah, swapping the order of the comparison in _Call.__eq__ causes the 
ANY.__eq__ to be used.

Michael

>
> Alex
>
> -- 
> "I disapprove of what you say, but I will defend to the death your 
> right to say it." -- Evelyn Beatrice Hall (summarizing Voltaire)
> "The people's good is the highest law." -- Cicero
>


-- 
http://www.voidspace.org.uk/

May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing http://www.sqlite.org/different.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120110/b7503f96/attachment.htm>


More information about the testing-in-python mailing list