问题描述
我正在尝试开始针对某些视图编写一些测试,但我的项目使用了 3rd 方登录系统,并且我有一个与此交互的自定义身份验证后端 BarBackend
。
我要测试的视图受 LoginrequiredMixin
保护。我想我将需要以某种方式修补登录测试客户端以访问这些并测试它们。
我的目标是创建一个用户,然后使用我打过补丁的后端登录该用户,以便我可以执行诸如 self.client.get('some-url-with-login-required')
之类的操作。
我该怎么做?到目前为止,我的尝试使我来到这里:
class FooTest(TestCase):
def setUp(self):
self.user = User.objects.create(username='user',email='test@example.com')
@patch('identity.backends.BarBackend.authenticate')
def test_can_access_with_groups(self,mock_auth):
mock_auth.return_value = self.user
self.client.login()
代码在 self.client.login
上失败。这是堆栈跟踪以防万一(删除/重命名了一些路径):
Traceback (most recent call last):
File "/usr/lib/python3.8/unittest/mock.py",line 1342,in patched
return func(*newargs,**newkeywargs)
File "project_name/apps_dir/nzplatform/tests/test_site_access.py",line 32,in test_can_access_with_groups
self.client.login()
File ".venv/lib/python3.8/site-packages/django/test/client.py",line 585,in login
user = authenticate(**credentials)
File ".venv/lib/python3.8/site-packages/django/contrib/auth/__init__.py",line 67,in authenticate
inspect.getcallargs(backend.authenticate,request,**credentials)
File "/usr/lib/python3.8/inspect.py",line 1336,in getcallargs
f_name = func.__name__
File "/usr/lib/python3.8/unittest/mock.py",line 637,in __getattr__
raise AttributeError(name)
AttributeError: __name__
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)