简体   繁体   English

错误单元测试ModelForm

[英]Error Unit testing a ModelForm

I am currently writing unit tests for my Django webapp and have come across a stumbling block. 我目前正在为Django Web应用程序编写单元测试,遇到了一个绊脚石。

My Model: 我的模特:

class Employment(models.Model):
    applicant = models.ForeignKey(Applicant)
    company = models.CharField(max_length=50)
    town = models.CharField(max_length=50)
    salary = models.CharField(max_length=50)
    job_title = models.CharField(max_length=50)
    responsibilities = models.CharField(max_length=100)
    date_from = models.DateField('Date From')
    date_to = models.DateField('Date To')
    reason_for_leaving = models.CharField(max_length=50)

My Form: 我的表格:

class EmploymentForm(forms.ModelForm):
    error_css_class = 'error'   #set some css when an error
    company = forms.CharField(label="Company/Agency")
    town = forms.CharField(label="Town/City")
    salary = forms.CharField(label="Salary/Pay rate")
    date_from = forms.DateField(('%d/%m/%Y',), widget=forms.DateInput(format='%d/%m/%Y', attrs={'placeholder':'dd/mm/yyyy'}))
    date_to = forms.DateField(('%d/%m/%Y',), widget=forms.DateInput(format='%d/%m/%Y', attrs={'placeholder':'dd/mm/yyyy'}))
    class Meta:
            model = Employment

My Test: 我的测试:

"""
Complete employment form passes validation
"""
def test_employment_form_complete(self):
    applicant = Applicant(job_id=1)
    data = {
    'company': 'test company',
    'town': 'test town',
    'salary': '1000000',
    'job_title': 'test job name',
    'responsibilities': 'test responsibilities',
    'date_from': '01/01/1990',
    'date_to': '01/02/1991',
    'reason_for_leaving': 'test reason for leaving'
    }
    employment_form = EmploymentForm(instance=applicant, data=data)
    result = employment_form.is_valid()
    print "errors %s" % employment_form.errors
    self.assertEqual(result, True)

If I run the test like this I get: 如果我像这样运行测试,我将得到:

<ul class="errorlist"><li>applicant<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

If I add: 如果我添加:

'applicant': applicant

to my data object it complains it need to be an int. 我的数据对象抱怨它必须是一个int。

If I add: 如果我添加:

'applicant': 1

(or another integer) (或另一个整数)

it returns: 它返回:

<ul class="errorlist"><li>applicant<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul></li></ul>

which is understandable really. 这确实是可以理解的。

How can I get around this? 我该如何解决? What is the best practise? 最佳实践是什么?

Regards, 问候,

Chris. 克里斯。

I think You should call applicant.save() 我认为您应该致电applicant.save()

You create new object, but did't save it to database. 您创建了新对象,但没有将其保存到数据库。 while testing django create "temporary" database. 在测试django时创建“临时”数据库。

Also add to data dictionary 'applicant': 1. 还要添加到数据字典'applicant':1。

PS. PS。 Make sure applicant object will be created with foreign key=1!! 确保将使用外键= 1创建申请人对象!

Completely working code. 完全有效的代码。


def test_employment_form_complete(self):
        """
        Complete employment form passes validation
        """
        applicant = Applicant(job_id=1)
        applicant.save()
        data = {
            'applicant': 1,
        'company': 'test company',
        'town': 'test town',
        'salary': '1000000',
        'job_title': 'test job name',
        'responsibilities': 'test responsibilities',
        'date_from': '01/01/1990',
        'date_to': '01/02/1991',
        'reason_for_leaving': 'test reason for leaving'
        }
        employment_form = EmploymentForm(instance=applicant, data=data)
        result = employment_form.is_valid()
        print "errors %s" % employment_form.errors
        self.assertEqual(result, True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM