1
我试图嘲弄在VKAuth类中的 “self.api.friends.get” 的方法:如何在django中模拟外部API?
import vk
class VKAuth(object):
def __init__(self, access_token, user):
self.session = vk.Session(access_token = access_token)
self.api = vk.API(self.session)
def follow(self):
vk_friends = self.api.friends.get()
从测试模块test_views.py:
from mock import patch
from ..auth_backends.vk_backend import VKAuth
class AddUsersToList(TestCase):
@patch.object(VKAuth.api.friends, 'get')
def test_auth_vk(self, mock_get):
... etc ...
我得到一个错误在测试期间:
AttributeError: <class 'accounts.auth_backends.vk_backend.VKAuth' doens't have the attribute 'api'
我在做什么错?如何在此类结构中访问此方法?
但是如果我想测试它不是直接将参数传递给类实例,而是使用初始化此实例的django-rest-framework?换句话说,如果测试方法和我检查响应,我会调用“response = self.client.post(reverse('app-social-auth',{”access_token“:”blablabla“})”。 – paus
您应该查找'social_auth'实例化你后端的地方,并嘲笑那个实例如果你想出了具体的例子,我会尽力帮助。 –
它在类AuthSocialView中的accounts.views中实例化为method“ auth_backends = {'vk':VKAuth,'脸谱':FBAuth}“ – paus