对具有发布请求和CKEditor表单的视图进行单元测试

时间:2019-01-07 14:51:44

标签: django unit-testing ckeditor

我有一个提交帖子的视图,它包含一个使用CKEditor作为该表单一部分的表单,这是 forms.py

中的表单
class CreateAdminPostForm(forms.ModelForm):
    content = forms.CharField(widget=CKEditorUploadingWidget())
    title = forms.CharField(
        widget=forms.TextInput(
            attrs={
                'placeholder': 'Post Title',
                'class': 'form-control',
                'style': 'max-width: 800px;',
                'maxlength': '160',
            }
        )
    )
    keyword = forms.CharField(
        widget=forms.TextInput(
            attrs={
                'placeholder': 'Focus Keyword',
                'maxlength': '60',
            }
        )
    )
    slug = forms.CharField(
        widget=forms.TextInput(
            attrs={
                'placeholder': 'Slug',
                'maxlength': '60',
            }
        )
    )
    description = forms.CharField(
        widget=forms.Textarea(
            attrs={
                'placeholder': 'Post Description , Will be used as the Meta description for the post page',
                'style': 'height: 370px;',
                'maxlength': '160',
            }
        )
    )

    class Meta:
        model = Post
        fields = ['title', 'content', 'slug', 'keyword', 'category', 'description', 'featured_image']

这是我在 views.py

中使用的视图
def admin_post(request):
    current_user = request.user
    current_author = Author.objects.get(user=current_user)
    create_admin_post_form = CreateAdminPostForm(request.POST, request.FILES)
    if request.method == 'POST':
        if create_admin_post_form.is_valid():
            form = create_admin_post_form.save(commit=False)
            form.active = True
            form.email_active = True
            form.author = current_author
            form.save()
            return redirect('home')
    else:
        create_admin_post_form = CreateAdminPostForm()
    context = {
        'create_admin_post_form': create_admin_post_form,
    }
    return render(request, 'admin_post.html', context)

最后,这就是我在测试

中对其进行测试的方式
class TestAdminPost(TestCase):
    def test_admin_post_view(self):
        response = self.client.get(reverse('admin_post'))
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, 'admin_post.html')

    def test_admin_post_empty(self):
        response = self.client.post(reverse('register'), {})
        self.assertFormError(response, 'create_admin_post_form', 'content', 'This field is required.')
        self.assertFormError(response, 'create_admin_post_form', 'title', 'This field is required.')
        self.assertFormError(response, 'create_admin_post_form', 'slug', 'This field is required.')
        self.assertFormError(response, 'create_admin_post_form', 'keyword', 'This field is required.')
        self.assertFormError(response, 'create_admin_post_form', 'category', 'This field is required.')
        self.assertFormError(response, 'create_admin_post_form', 'description', 'This field is required.')
        self.assertFormError(response, 'create_admin_post_form', 'featured_image', 'This field is required.')

    def test_admin_post_valid(self):
        content = 'this is a test content for the post'
        title = ' also this is a test title for the great post '
        slug = 'test-slug'
        keyword = 'test keyword'
        category = Category.objects.create(name='testing category', slug='testing-category')
        description = 'this is the demo description for the test post'
        image = SimpleUploadedFile(name='test_image.jpg', content=open('author.png', 'rb').read(),
                                   content_type='image/png')
        response = self.client.post(reverse('register'),
                                    {'content': content, 'title': title, 'slug': slug, 'keyword': keyword,
                                     'category': category, 'description': description, 'image': image})
        self.assertEqual(response.status_code, 302)

测试中,我使用了get请求来测试模板和响应,然后我使用了2个post请求,其中一个包含空数据,另一个具有有效数据

尝试测试时出现此错误:

我在使用我的CKEditor self.assertFormError(response, 'create_admin_post_form', 'content', 'This field is required.') 的第AssertionError: The form 'create_admin_post_form' was not used to render the response行出现了错误

来自test_admin_post_empty部分

也收到此错误

AssertionError: 200 != 302来自test_admin_post_valid错误

0 个答案:

没有答案