[TIP] hasattr and Mock
Jacocks, Jeremy
jjacocks at cars.com
Thu Aug 25 14:28:25 PDT 2011
Great info, thank you!
On Aug 25, 2011, at 10:58 AM, Michael Foord wrote:
> On 25/08/2011 18:26, Jacocks, Jeremy wrote:
>> This may be a known issue, but is there a good workaround to hasattr always returning true for a mocked object?
>>
>> Any links or documentation on the subject would be helpful,
>
> An example:
>
>>>> from mock import Mock
>>>> m = Mock(spec=['foo'])
>>>> hasattr(m, 'bar')
> False
>
> spec can also be an object (like a class), in which case the list of
> allowed members is taken from the dir of the object you pass in.
>
> You can get this behaviour automatically with patch if you do:
>
> with patch('foo.bar.baz', spec=True):
> ...
>
> The created mock will have the spec of the object it is replacing.
>
> All the best,
>
> Michael Foord
>
>> Thank you!
>>
>> Jeremy
>>
>> _______________________________________________
>> testing-in-python mailing list
>> 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 blessing http://www.sqlite.org/different.html
>
More information about the testing-in-python
mailing list