【问题标题】:I am geeting error in unit testing of django app我在 django 应用程序的单元测试中遇到错误
【发布时间】:2020-09-05 05:35:28
【问题描述】:

我收到错误“ValueError:无法分配“'12A20'”:“Attendance.rollno”必须是“学生”实例。” 怎么解决?? Django中存在外键时是否有另一种测试方法? 模型.py

 class Student(models.Model):
     classId=models.ForeignKey(Class,on_delete=models.CASCADE)
     name=models.CharField(max_length=20)
     fatherName=models.CharField(max_length=20)
     motherName=models.CharField(max_length=20)
     address=models.TextField(max_length=100)
     section = models.CharField(max_length=2)
     prevClass=models.IntegerField()
     prevClassMark=models.IntegerField()
     prevResult=models.ImageField(upload_to='images/')
     gender=models.CharField(max_length=6)
     image=models.ImageField(upload_to='images/')
     stream=models.CharField(max_length=10,)#choices=Stream)
     department=models.CharField(max_length=15)
     dob=models.CharField(max_length=12)
     rollno = models.CharField(max_length=10)
     password=models.CharField(max_length=30)

  class Attendance(models.Model):
     rollno=models.ForeignKey(Student,on_delete=models.CASCADE)
     class_id=models.ForeignKey(Class,on_delete=models.CASCADE)
     date=models.CharField(max_length=11)
     status=models.CharField(max_length=7)

tests.py

class AttendanceTest(TestCase):
    def setUp(self):
        Attendance.objects.create(
        rollno='12A20',
        class_id=121,
        date='2020-09-03',
        status='Present'
        )
        Attendance.objects.create(
        rollno='13A20',
        class_id=121,
        date='2020-09-03',
        status='Present'
        )
    def test_Attendance(self):
        qs=Attendance.objects.all()
        self.assertEqual(qs.count(),2)

【问题讨论】:

    标签: python django web


    【解决方案1】:

    添加 Student 实例代替 rollno。

    class AttendanceTest(TestCase):
        def setUp(self):
            Attendance.objects.create(
            rollno=Student.objects.filter(rollno='12A20').first(),
            class_id=121,
            date='2020-09-03',
            status='Present'
            )
            Attendance.objects.create(
            rollno=Student.objects.filter(rollno='13A20').first(),
            class_id=121,
            date='2020-09-03',
            status='Present'
            )
        def test_Attendance(self):
            qs=Attendance.objects.all()
            self.assertEqual(qs.count(),2)
    

    【讨论】:

      【解决方案2】:

      您已对 rollno='12A20' 进行硬编码,因为这是不允许的,这就是您收到此错误的原因。

      一些附加信息,如果您在开发空间中进行单元测试,Django 默认不使用您的生产数据库。

      如果您想了解有关为模型编写单元测试的更多信息:查看官方 Django 文档this link

      对于你的场景,我会做这样的事情:

      class AttendanceTest(TestCase):
          def setUp(self):
              
              # create student and class instance/object
              student = Student.objects.create(name = 'Anirduh')
              # You haven't given us information about the Class class
              class = Class.objects.create(...........)
      
              Attendance.objects.create(
                 rollno=student, # just pass in the student instance/object
                 class_id=class, # just pass in the class instance/object
                 date='2020-09-03',
                 status='Present'
              )
      

      【讨论】:

        猜你喜欢
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-14
        • 1970-01-01
        • 2013-01-03
        • 2021-10-29
        相关资源
        最近更新 更多