3
셀록 작업 호출을 조롱 된 반환 값으로 패치하면 return_value
이 mock_task.get.return_value = "value"
으로 정의 된 대신 <Mock name='mock().get()' ...>
을 반환합니다. 그러나 조롱 된 작업은 내 단위 테스트에서 올바르게 작동합니다. 함수가 테스트되고 여기패치로
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__'
을 제기 여기
foo
에서 호출하면 예상 된 반환 값 대신
<Mock ...>
가 반환됩니까?