6👍
✅
You are patching the wrong thing. In VKAuth
:
self.api = vk.API(self.session)
add api
attribute to VKAuth
self
object. When you call
patch.object(accounts.auth_backends.vk_backend.VKAuth, 'api')
you are patching the api
static attribute of VKAuth
class and not the object attribute.
You should patch vk.API
instead.
with patch('vk.API', autospec=True) as mock_api:
response = self.client.post(reverse('auth-social', kwargs=dict(backend='vk')), dict(access_token=auth_token), follow=True)
Notes :
- Use
patch.object
only if you really know why you need it instead simplepatch
. autospec=True
is not mandatory but I strongly encourage to use it.- In the
patch
contextself.api
will be equal tomock_api.return_value
because callvk.API(self.session)
is like callmock_api()
; in other wordsmock_api
is the mock object used to replace thevk.API
reference. - Take a look to where to patch, you can find it very useful.
Now if you want fill your mock_api.return_value
by some behavior you can configure it in with
context:
with patch('vk.API', autospec=True) as mock_api:
m_api = mock_api.return_value
m_api.friends.return_value = None
m_api.friends.get.return_value = vk_ids
.... Your test
Source:stackexchange.com