This is a example I have a class :
engine/a.py
from thirdlib import TaskClient
class TaskWorker(object):
def __init__(self):
self.task_client = TaskClient(xxx, xxx, xxx, xxx)
def some_task(id, app):
tasks = self.task_client.query(1,2,3,4)
print "tasks: " tasks
for task in tasks:
do_something()
then I want to test it:
class ImporterTest(unittest.TestCase):
def setUp(self):
self.client_patcher = patch('engine.a.TMClient', autospec=True)
self.client_mocked = self.client_patcher.start()
self.worker = TaskWorker()
def tearDown(self):
self.client_patcher.stop()
def test_task_return_empty(self):
self.client_mocked.query.return_value = []
value = self.worker.some_task(1, 'notcare')
#self.assertFalse(value)
#print self.client_mocked.mock_calls
self.client_mocked.query.assert_called_with(1,2,3,4)
This test will failed with query.assert_called_with,
Tasks: <MagicMock name='TMClient().query()' id='139779549447120'>
Traceback (most recent call last):
File "/home/xxx/lib/python2.7/site-packages/mock/mock.py", line 925, in assert_called_with
raise AssertionError('Expected call: %s\nNot called' % (expected,))
AssertionError: Expected call: query(1,2,3,4) Not called
the print expected "Tasks: []" but not , as the assert_called_with is failed also