I am trying to mock an instance method in Python with a side_effect. I want/expect my side effect to be called with an initial 'self' argument that I can use to determine the return value.
So I have something like this:
import mock
class TestCases(unittest.TestCase):
@mock.patch('Item.exists')
def test_foo(self, mock_item_exists):
def item_exists_side_effect(*args, **kwargs):
# I expect args[0] here to be supplied and to refer to the item
_self = args[0]
return _self.name == 'bar'
mock_item_exists.side_effect = item_exists_side_effect
...
However, when Item.exists() gets called I end up in my side-effect function but with an empty args list.
Is this expected? Am I doing something wrong?
Looks like this is the reason:
Why does mock ignore the instance/object passed to a mocked out method when it is called?
Ie by calling mock.patch I turned the instance method into a class method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.