如何在Django中为UserProfile表单创建测试

时间:2019-09-22 23:40:02

标签: django forms testcase

我是Django的新手,也不知道自己在做什么错。 我正在尝试为我的表单“ UserProfileForm”运行测试,并且该测试一直失败,并且我不知道为什么。有人可以帮忙吗?

这是我的表格。py

from .models import UserProfile
from dobwidget import DateOfBirthWidget


class UserProfileForm(forms.ModelForm):

    class Meta:
        model = UserProfile
        fields = [
            'profile_img',
            'subscribe',
            'bio',
            'dob',
            'telephone',
            'contact_by_phone',
            'contact_by_email'
            ]
        widgets = {
            'dob': DateOfBirthWidget(),
        }

这是我的模型。py

from django.contrib.auth.models import User



class UserProfile(models.Model):


    user = models.OneToOneField(User, on_delete=models.CASCADE)

    profile_img = models.ImageField(upload_to='images', default="images/default_profile.png")
    subscribe = models.BooleanField(blank=True, default=False)
    bio = models.TextField(blank=True)
    dob = models.DateField(max_length=8, null=True, blank=True)
    telephone = models.CharField(max_length=20, null=True, blank=True)
    contact_by_phone = models.BooleanField(blank=True, default=False)
    contact_by_email = models.BooleanField(blank=True, default=False)


    def __str__(self):
        return self.user.username 

这是我要运行的测试

from django.test import TestCase
from .forms import UserProfileForm



class TestUserProfileForm(TestCase):


    def test_can_create_a_profile(self):
        valid_data = {
            'profile_img':"images/perfil.jpg", 
            'subscribe':False, 
            'bio':'asdjklsad asdkj;s', 
            'dob':'2019-03-03',
            'telephone' : '12324',
            'contact_by_phone' : True,
            'contact_by_email' : True,
        }
        form = UserProfileForm(data=valid_data)
        self.assertTrue(form.is_valid())

0 个答案:

没有答案
相关问题