3
修补具有模拟返回值的Celery任务调用返回<Mock name='mock().get()' ...>
而不是mock_task.get.return_value = "value"
定义的预期return_value
。但是,模拟任务在我的单元测试中正确运行。用Python修补Celery任务调用,修补程序为
这里就是我修补我的芹菜任务单元测试:
def test_foo(self):
mock_task = Mock()
mock_task.get = Mock(return_value={'success': True})
print mock_task.get() # outputs {'success': True}
with patch('app.tasks.my_task.delay', new=mock_task) as mocked_task:
foo() # this calls the mocked task with an argument, 'input from foo'
mock_tasked.assert_called_with('input from foo') # works
,这里是正在测试的功能:
def foo():
print tasks.my_task.delay # shows a Mock object, as expected
# now let's call get() on the mocked task:
task_result = tasks.my_task.delay('input from foo').get()
print task_result # => <Mock name='mock().get()' id='122741648'>
# unexpectedly, this does not return {'success': True}
if task_result['success']:
...
最后一行引发TypeError: 'Mock' object has no attribute '__getitem__'
为什么能我从我的单元测试中调用mock_task.get(),但从foo
调用它会返回<Mock ...>
而不是预期的返回值?