Django - unit test AssertionError: 302 != 200, decorator problem
Django - unit test AssertionError: 302 != 200, decorator problem
我正在尝试为我的 Django 应用程序编写测试。测试失败,因为我收到 302 错误。对于 CBV,我使用了一个 decorator.py 文件来检查用户 is_teacher
。我相信这就是导致问题的原因。
查看
@method_decorator([login_required, teacher_required], name='dispatch')
class ClassroomList(ListView):
model = Classroom
def get_queryset(self):
return Classroom.objects.filter(user=self.request.user).order_by('classroom_name')
型号
class CustomUser(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
is_student = models.BooleanField('student status', default=False)
is_teacher = models.BooleanField('teacher status', default=False)
def __str__(self):
return self.username
test.py
class GradebookTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(
username='tester',
email='tester@email.com',
password='tester123'
)
self.user.is_active = True
self.user.is_teacher = True
def test_classroomgradebook(self):
self.client.login(username='tester', password='tester123')
response = self.client.get(reverse('gradebook:gradebookstart'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Choose the gradebook')
self.assertNotContains(response, 'Enter')
self.assertTemplateUsed(response, 'gradebook/classroom_list.html')
decorator.py
def teacher_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='login'):
actual_decorator = user_passes_test(
lambda u: u.is_active and u.is_teacher,
login_url=login_url,
redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator
如果我从视图中删除 @method_decorator...
,则测试通过。
如@Brian Destur所述,您需要调用模型的save()
方法来提交更改数据库
class GradebookTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(
username='tester',
email='tester@email.com',
password='tester123'
)
self.user.is_active = True
self.user.is_teacher = True
<b>self.user.save() # Save the changes to DB</b>
# your code
我正在尝试为我的 Django 应用程序编写测试。测试失败,因为我收到 302 错误。对于 CBV,我使用了一个 decorator.py 文件来检查用户 is_teacher
。我相信这就是导致问题的原因。
查看
@method_decorator([login_required, teacher_required], name='dispatch')
class ClassroomList(ListView):
model = Classroom
def get_queryset(self):
return Classroom.objects.filter(user=self.request.user).order_by('classroom_name')
型号
class CustomUser(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
is_student = models.BooleanField('student status', default=False)
is_teacher = models.BooleanField('teacher status', default=False)
def __str__(self):
return self.username
test.py
class GradebookTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(
username='tester',
email='tester@email.com',
password='tester123'
)
self.user.is_active = True
self.user.is_teacher = True
def test_classroomgradebook(self):
self.client.login(username='tester', password='tester123')
response = self.client.get(reverse('gradebook:gradebookstart'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Choose the gradebook')
self.assertNotContains(response, 'Enter')
self.assertTemplateUsed(response, 'gradebook/classroom_list.html')
decorator.py
def teacher_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='login'):
actual_decorator = user_passes_test(
lambda u: u.is_active and u.is_teacher,
login_url=login_url,
redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator
如果我从视图中删除 @method_decorator...
,则测试通过。
如@Brian Destur所述,您需要调用模型的save()
方法来提交更改数据库
class GradebookTests(TestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(
username='tester',
email='tester@email.com',
password='tester123'
)
self.user.is_active = True
self.user.is_teacher = True
<b>self.user.save() # Save the changes to DB</b>
# your code