我有要测试的代码(简化):
def foo(job):
stop1 = job.stops.get(stop_number=1)
stop1.departed_at = first_scan['scan_date']
stop2 = job.stops.get(stop_number=2)
stop2.pod_time = pod['received_date']
所以我这样做:
job = Mock()
foo(job)
self.assertIsInstance(job.sync_at, datetime.datetime)
self.assertAlmostEqual(job.booked_at, isoformat('2019-10-01T00:00:00'))
self.assertAlmostEqual(job.stops.get(stop_number=1).departed_at, isoformat('2019-10-07T05:24:47'))
self.assertAlmostEqual(job.stops.get(stop_number=2).pod_time, isoformat('2019-10-07T12:00:00'))
问题在于job.stops.get方法返回带有任何参数值的相同对象:
>>> job.stops.get(stop_number=1)
<Mock name='mock.stops.get()' id='140194394261936'>
>>> job.stops.get(stop_number=2)
<Mock name='mock.stops.get()' id='140194394261936'>
>>>
因此,在测试上述代码时,我同时job.stops.get(stop_number=1)
和job.stops.get(stop_number=2)
返回了设置了departed_at
和pod_time
属性的相同输出。因此,我不确定pod_time
是否设置为stop2而不是stop1,反之亦然。
问题是如何使mock
返回与stop_number
值有关的不同实例?
答案 0 :(得分:0)
您可以将可调用对象分配给Mock
对象的side_effect
属性。根据不同的输入参数使可调用函数返回不同的对象:
def mock_get(stop_number):
return {
1: Mock(departed_at='2019-10-07T05:24:47'),
2: Mock(pod_time='2019-10-07T12:00:00')
}[stop_number]
job = Mock()
job.stops.get.side_effect = mock_get
foo(job)
assert job.stops.get(stop_number=1).departed_at == '2019-10-07T05:24:47' # passes
assert job.stops.get(stop_number=2).pod_time == '2019-10-07T12:00:00' # passes