Python 模拟补丁程序由于某些原因而失败

Python 模拟补丁程序由于某些原因而失败,python,mocking,Python,Mocking,我正在尝试为单元测试目的修补一个API。我有一门课是这样的: # project/lib/twilioclient.py from os import environ from django.conf import settings from twilio.rest import TwilioRestClient def __init__(self): return super(Client, self).__init__( settings.T

我正在尝试为单元测试目的修补一个API。我有一门课是这样的:

# project/lib/twilioclient.py
from os import environ
from django.conf import settings
from twilio.rest import TwilioRestClient

    def __init__(self):
        return super(Client, self).__init__(
            settings.TWILIO_ACCOUNT_SID,
            settings.TWILIO_AUTH_TOKEN,
        )

client = Client()
这种方法:

# project/apps/phone/models.py
from project.lib.twilioclient import Client
# snip imports

class ClientCall(models.Model):
    'call from Twilio'
    # snip model definition

    def dequeue(self, url, method='GET'):
        '''"dequeue" this call (take off hold)'''
        site = Site.objects.get_current()
        Client().calls.route(
            sid=self.sid,
            method=method,
            url=urljoin('http://' + site.domain, url)
        )
然后进行以下测试:

# project/apps/phone/tests/models_tests.py
class ClientCallTests(BaseTest):
    @patch('project.apps.phone.models.Client', autospec=True)
    def test_dequeued(self, mock_client):
        cc = ClientCall()
        cc.dequeue('test', 'TEST')

        mock_client.calls.route.assert_called_with(
            sid=cc.sid,
            method='TEST',
            url='http://example.com/test',
        )
当我运行测试时,它失败了,因为调用的是真实客户机而不是模拟客户机


看起来对我来说应该行得通,我以前也做过这种事情。我尝试过常见的怀疑(删除
*.pyc
),但情况似乎没有改变。这里是否有一种新的行为我不知怎么错过了?

有没有电话?或者没有电话…肯定有电话-打什么?测试调用
ClientCall.dequeue
,它实例化
客户机
Client
应该是一个Mock,但它不是。或者检查
Mock\u Client.Mock\u调用
如果我出列失败(
assert False
)并首先打印“Client”,我会得到一个
twilio.rest.TwilioRestClient
。这就是你所说的“调试”吗?我的意思是使用
importpdb;pdb.set_trace()