We create mock objects and specify the behavior of methods called on them:
from unittest.mock import patch, Mock
execute = Mock()
execute.fetchone = Mock(return_value={'state': json.dumps({})})
with patch('modules.data_chat.chatbot.execute', new=lambda x, y, z: execute):
auto_reply(None, "test_user_id", "Hello?", 1)
assert execute.fetchone.called
assert execute.called
code under test (auto_reply) implementation:
from tools.db_tools import execute
[...]
cur = execute(conn, sql, (bot_id, channel_id))
state = cur.fetchone()
Back in the test code, execute.fetchone.called == True. However, execute.called == False
Why is execute.called == False and how can I fix this? Is new=lambda x, y, z: execute correct?