我正在尝试为单元测试目的修补 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
)并且事情似乎没有改变。这里是否有我以某种方式遗漏的新行为?