Python mock patch未重置返回值

3

我正在使用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模拟补丁来重置该值。

1个回答

1
不知道确切原因,但似乎需要导入正在测试的函数/类的父级。我在views.py中写了这行代码。
from user_directory.models import Contact

mock.patch对联系人没有影响。可以在这里看到一个例子。因此,我将我的代码更改为以下内容,并且它非常有效。

def test_no_content_found_with_patch(self):
    print "this function will mock Contact model get_by_phone to return none"
    with mock.patch('user_directory.models.Contact.get_by_phone') as fake_func:
        fake_func.return_value = None
        resp = self.client.get(self.get_address_by_phone, {'phone_no' : 1234567891})
        self.assertTrue(resp.status_code == 204)

def test_success_with_patch(self):
    print  "this function will test the address by phone view after mocking model"
    with mock.patch('user_directory.models.Contact.get_by_phone') as fake_func:
        contact_obj = Contact(recent_address_id = 123, best_address_id = 457)
        fake_func.return_value = contact_obj
        resp = self.client.get(self.get_address_by_phone, {'phone_no' : 1234567891})
        resp_body = json.loads(resp.content)
        self.assertTrue(resp_body == {  'recent_address_id' : contact_obj.recent_address_id, 
                                        'frequent_address_id' : 457
                                    }
                    )

看到这行
with mock.patch('user_directory.models.Contact.get_by_phone') as fake_func

这应该可以正常工作,但原始帖子也应该能够正常工作。请在您能够使原始帖子按原样工作时进行更新,谢谢。 - Gang
@Gang 上面的方法不起作用。如果在我的视图中执行以下操作: 从user_directory导入models, 然后使用 models.Contact代替Contact,上面的方法就可以起作用了。 - Anubhav Agarwal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接