我如何模拟可迭代项?
尝试测试此方法时:
def convert_time_field(klass, field, default=None):
time = GrapheneHelper.get_json_field(klass, field, default)
if time is None:
return None
else:
return iso8601.parse_date(time)
我的第一个测试返回此错误:TypeError: argument of type 'Mock' is not iterable
def test_convert_time_field(self):
""" Return YYYY-MM-DDTHH:MM:FF in iso8601 format """
time = '2017-09-18T23:09:57Z'
expected_time = '2017-09-18 23:09:57+00:00'
mock_klass = Mock()
self.assertEqual(GrapheneHelper.convert_time_field(mock_klass, time, default=None), expected_time)
在这种情况下,我对嘲笑有什么误解?
答案 0 :(得分:0)
我的测试最终改为:
@patch('myModule.myClass')
def test_convert_time_field(self, mock_klass):
""" Return YYYY-MM-DDTHH:MM:FF in iso8601 format """
mock_klass.json = {'time_field': '2017-09-18T23:09:33'}
expected_time = iso8601.parse_date('2017-09-18 23:09:33')
self.assertEqual(GrapheneHelper.convert_time_field(mock_klass, field='time_field', default=None), expected_time)