请帮帮我。我目前的任务是为登录方法创建一个测试脚本。
这是我正在测试的登录方法......
class AuthViewModel():
fixture = [user]
user_name = 'usera'
password = '12345678'
def login_page(self, userName, password, request):
"""
Login by ID & PWD
"""
# Get user by name & password
self.user = authenticate(username=userName, password=password)
if self.user is not None:
if self.user.is_active:
# Login by Django
login(request, self.user)
else:
# User not active
self.message = "User is not actived yet"
else:
# User not exist
self.message = "User name or password is incorrect"
这是我做的测试脚本。
def test_login_page(self):
"""Test log in
"""
actauth = AuthViewModel()
actauth.actinbox_login(self.user_name, self.password, request)
self.assertEqual(actauth.message, 'User name or password is incorrect')
这是我的问题,我收到了错误消息
NameError: name 'request' is not defined
如何定义'请求'?
答案 0 :(得分:1)
您需要使用RequestFactory创建一个请求对象。
RequestFactory与测试客户端共享相同的API。然而, RequestFactory提供了一种方式,而不是像浏览器一样 生成可用作第一个参数的请求实例 任何观点。这意味着您可以像测试视图功能一样 你会测试任何其他功能 - 作为一个黑盒子,完全知道 输入,测试特定输出。
所以基本上
factory = RequestFactory()
request = factory.get('/your/login/page/')
actauth = AuthViewModel()
actauth.actinbox_login(self.user_name, self.password, request)
self.assertEqual(actauth.message, 'User name or password is incorrect')