Please help me. I am currently tasked to create a test script for the login method.
请帮助我。我目前的任务是为登录方法创建一个测试脚本。
This is the login method that I am testing...
这是我正在测试的登录方法……
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"
And this is the test script that I did.
这是我做的测试脚本。
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')
This is my problem, I received error message
这是我的问题,我收到了错误信息。
NameError: name 'request' is not defined
how to define 'request' ?
如何定义“请求”?
1 个解决方案
#1
1
You need to create a request object with a RequestFactory.
您需要使用RequestFactory创建一个请求对象。
The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view. This means you can test a view function the same way as you would test any other function – as a black box, with exactly known inputs, testing for specific outputs.
RequestFactory与测试客户机共享相同的API。然而,RequestFactory提供了一种方法来生成一个请求实例,该实例可以作为任何视图的第一个参数使用。这意味着您可以像测试任何其他函数一样测试视图函数——作为一个黑盒子,使用完全已知的输入,测试特定的输出。
So basically
所以基本上
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')
#1
1
You need to create a request object with a RequestFactory.
您需要使用RequestFactory创建一个请求对象。
The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view. This means you can test a view function the same way as you would test any other function – as a black box, with exactly known inputs, testing for specific outputs.
RequestFactory与测试客户机共享相同的API。然而,RequestFactory提供了一种方法来生成一个请求实例,该实例可以作为任何视图的第一个参数使用。这意味着您可以像测试任何其他函数一样测试视图函数——作为一个黑盒子,使用完全已知的输入,测试特定的输出。
So basically
所以基本上
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')