我正在尝试修补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
),事情似乎没有改变。这里有一种新的行为,我以某种方式失踪了吗?