所以,我刚开始使用模拟Django项目。我试图模拟出一部分视图,它向远程API发出请求,以确认订阅请求是真实的(根据我正在使用的规范进行的验证形式)。Python模拟,Django和请求
我有什么似的:
class SubscriptionView(View):
def post(self, request, **kwargs):
remote_url = request.POST.get('remote_url')
if remote_url:
response = requests.get(remote_url, params={'verify': 'hello'})
if response.status_code != 200:
return HttpResponse('Verification of request failed')
我现在想要做的就是使用模拟来模拟出requests.get
呼叫改变的反应,但我不能工作,如何为做到这一点补丁装饰者。我以为你会这样做:
@patch(requests.get)
def test_response_verify(self):
# make a call to the view using self.app.post (WebTest),
# requests.get makes a suitable fake response from the mock object
我该如何做到这一点?
使用模拟的死亡?还有django.test.client.RequestFactory - https://docs.djangoproject.com/en/1.5/topics/testing/advanced/#module-django.test.client – David
只为未来的观众,提问者想模拟外部API调用。不是对视图本身的调用。在这种情况下,嘲笑似乎非常明智。 – aychedee
按照@aychedee,这确实是我在这个问题上所追求的目标。 – jvc26