我正在使用Python Mock库编写测试用例。
class AddressByPhoneTestCase(TestCase):
def test_no_content_found_with_mock(self):
print "this function will mock Contact model get_by_phone to return none"
with mock.patch('user_directory.models.Contact') as fake_contact:
print "fake_contact_id ", id(fake_contact)
conf = { 'get_by_phone.return_value': None }
fake_contact.configure_mock(**conf)
resp = self.client.get(reverse('get_address_by_phone'), {'phone_no' : 1234567891})
self.assertTrue(resp.status_code == 204)
def test_success_with_mock(self):
print "this function will test the address by phone view after mocking model"
with mock.patch('user_directory.models.Contact') as fake_contact:
print "fake_contact_id ", id(fake_contact)
contact_obj = Contact(recent_address_id = 123, best_address_id = 456)
conf = { 'get_by_phone.return_value': contact_obj }
fake_contact.configure_mock(**conf)
resp = self.client.get(reverse('get_address_by_phone'), {'phone_no' : 1234567891})
resp_body = json.loads(resp.content)
self.assertTrue(resp_body == { 'recent_address_id' : 123,
'frequent_address_id' : 456
}
)
在第二种情况下,即使我将Contact.get_by_phone更改为返回contact_obj,它仍然返回None。当我删除上面的测试用例时,这个测试用例通过了,但是否则会引用上述原因而失败。有人可以帮忙吗?我该如何使用Python模拟补丁来重置该值。