用于删除 post returns Https 200 而不是 302 的 Django 测试用例
Django Test case for deleting a post returns Https 200 instead of 302
我正在为我的 Django 项目中的 Posts 应用编写测试用例。我定义了用于添加新 post、列出 post、查看 post 的详细信息、编辑 post 和删除 post 的视图。
添加新的 post 会将页面重定向到该页面的详细视图,删除 post 会将页面重定向到列表视图。在为此编写测试时,我得到了预期的新 post 的 https 响应代码 302,但是对于删除 post,我只得到了 https 响应 200,而它应该是 302。
在下面添加必要的代码。如果需要,请随时询问与此相关的更多代码。
有什么概念是我遗漏的吗?欢迎任何潜在客户。
提前致谢。
Test.py
from django.test import TestCase
from .models import Post
from django.urls import reverse, reverse_lazy
class PostTest(TestCase):
def setUp(self):
'''This function inserts
dummy data into database
to check during testing'''
self.post = Post.objects.create(title='Test', content='abcd', author='testauthor@testing.test')
def test_post_content(self):
post_object = Post.objects.get(pk=1)
expected_object_title = f'{post_object.title}'
expected_object_content = f'{post_object.content}'
expected_object_author = f'{post_object.author}'
self.assertEqual(expected_object_title,'Test')
self.assertEqual(expected_object_content, 'abcd')
return self.assertEqual(expected_object_author, 'testauthor@testing.test')
def test_post_list_view(self):
response = self.client.get(reverse('lists'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Test')
self.assertTemplateUsed(response, 'index.html')
def test_post_details(self):
response = self.client.get('/detail/1/')
no_response = self.client.get('/detail/100/')
self.assertEqual(response.status_code, 200)
self.assertEqual(no_response.status_code, 404)
self.assertContains(response, 'abcd')
self.assertTemplateUsed(response, 'detailed.html')
def test_newpost_view(self):
response = self.client.post(reverse('addpost'),{
'title' : 'New title',
'content' : 'New Content',
'author' : 'test@gmail.com',
})
self.assertEqual(response.status_code, 302)
#self.assertContains(response,'New Content')
def test_post_update_view(self):
response = self.client.post(reverse('update', args='1'),{
'title': 'Updated title',
'content' : 'Updated content',
})
self.assertContains(response, 'Updated title')
self.assertEqual(response.status_code, 200)
def test_postdelete_view(self):
response = self.client.get(reverse('delete',args='1'))
self.assertEqual(response.status_code,302)
views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView,UpdateView, DeleteView
from django.urls import reverse,reverse_lazy
from .models import Post
class ListingView(ListView):
model = Post
template_name = 'index.html'
context_object_name = 'list_of_posts'
class DetailedListView(DetailView):
model = Post
template_name = 'detailed.html'
class AddPostView(CreateView):
model = Post
template_name = 'addpost.html'
fields = ['title','author', 'content']
class PostUpdateView(UpdateView):
model = Post
template_name = 'update.html'
fields = ['title', 'author', 'content']
class DeletePostView(DeleteView):
model = Post
template_name = 'deleteview.html'
success_url = reverse_lazy('lists')
Models.py
from django.db import models
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
author = models.EmailField()
def __str__(self):
return self.title[:30]
def get_absolute_url(self):
return reverse('detailed', args=[str(self.id)])
response = self.client.get(reverse('delete',args='1'))
使用 GET
请求,您要求的模板 'deleteview.html'
需要填写才能删除对象。因此,状态代码 200
是正确的。
您应该使用POST
(或DELETE
)请求。这将删除对象并重定向到 success_url = reverse_lazy('lists')
。因此,您将获得状态代码 302
.
想清楚的看一下DeleteView的代码。
我正在为我的 Django 项目中的 Posts 应用编写测试用例。我定义了用于添加新 post、列出 post、查看 post 的详细信息、编辑 post 和删除 post 的视图。 添加新的 post 会将页面重定向到该页面的详细视图,删除 post 会将页面重定向到列表视图。在为此编写测试时,我得到了预期的新 post 的 https 响应代码 302,但是对于删除 post,我只得到了 https 响应 200,而它应该是 302。
在下面添加必要的代码。如果需要,请随时询问与此相关的更多代码。
有什么概念是我遗漏的吗?欢迎任何潜在客户。
提前致谢。
Test.py
from django.test import TestCase
from .models import Post
from django.urls import reverse, reverse_lazy
class PostTest(TestCase):
def setUp(self):
'''This function inserts
dummy data into database
to check during testing'''
self.post = Post.objects.create(title='Test', content='abcd', author='testauthor@testing.test')
def test_post_content(self):
post_object = Post.objects.get(pk=1)
expected_object_title = f'{post_object.title}'
expected_object_content = f'{post_object.content}'
expected_object_author = f'{post_object.author}'
self.assertEqual(expected_object_title,'Test')
self.assertEqual(expected_object_content, 'abcd')
return self.assertEqual(expected_object_author, 'testauthor@testing.test')
def test_post_list_view(self):
response = self.client.get(reverse('lists'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'Test')
self.assertTemplateUsed(response, 'index.html')
def test_post_details(self):
response = self.client.get('/detail/1/')
no_response = self.client.get('/detail/100/')
self.assertEqual(response.status_code, 200)
self.assertEqual(no_response.status_code, 404)
self.assertContains(response, 'abcd')
self.assertTemplateUsed(response, 'detailed.html')
def test_newpost_view(self):
response = self.client.post(reverse('addpost'),{
'title' : 'New title',
'content' : 'New Content',
'author' : 'test@gmail.com',
})
self.assertEqual(response.status_code, 302)
#self.assertContains(response,'New Content')
def test_post_update_view(self):
response = self.client.post(reverse('update', args='1'),{
'title': 'Updated title',
'content' : 'Updated content',
})
self.assertContains(response, 'Updated title')
self.assertEqual(response.status_code, 200)
def test_postdelete_view(self):
response = self.client.get(reverse('delete',args='1'))
self.assertEqual(response.status_code,302)
views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView,UpdateView, DeleteView
from django.urls import reverse,reverse_lazy
from .models import Post
class ListingView(ListView):
model = Post
template_name = 'index.html'
context_object_name = 'list_of_posts'
class DetailedListView(DetailView):
model = Post
template_name = 'detailed.html'
class AddPostView(CreateView):
model = Post
template_name = 'addpost.html'
fields = ['title','author', 'content']
class PostUpdateView(UpdateView):
model = Post
template_name = 'update.html'
fields = ['title', 'author', 'content']
class DeletePostView(DeleteView):
model = Post
template_name = 'deleteview.html'
success_url = reverse_lazy('lists')
Models.py
from django.db import models
from django.urls import reverse
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
author = models.EmailField()
def __str__(self):
return self.title[:30]
def get_absolute_url(self):
return reverse('detailed', args=[str(self.id)])
response = self.client.get(reverse('delete',args='1'))
使用 GET
请求,您要求的模板 'deleteview.html'
需要填写才能删除对象。因此,状态代码 200
是正确的。
您应该使用POST
(或DELETE
)请求。这将删除对象并重定向到 success_url = reverse_lazy('lists')
。因此,您将获得状态代码 302
.
想清楚的看一下DeleteView的代码。