Question

there is a patch.dict function for mocking a dictionary. Is it possible to mock a list with the mock 1.0.1 library? I am not going to call on the mocked object, so side_effect would not work for me.

Was it helpful?

Solution

There is no special method for lists like there is for dicts, but you can use the general patch method:

from mock import patch
foo = ['original', 'stuff']
with patch('__main__.foo', new=['mocked', 'items']):
    assert foo == ['mocked', 'items']
assert foo == ['original', 'stuff']

Note that the first argument to patch is a string with the full Python path to the item you want to patch. If your list is as module scope, you can use 'package.module.<list name>'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top