简体   繁体   English

我正在尝试使用 Django 测试与 Inspection and Defect 的关系,并在 tests.py 中出错

[英]I am trying to test a relationship with Inspection and Defect using Django and got an error in tests.py

I am trying to do a fun project that relates Inspections and Defects.我正在尝试做一个与检查和缺陷相关的有趣项目。
The error I got was Inspection() got an unexpected keyword argument partNum.我得到的错误是 Inspection() 得到了一个意外的关键字参数 partNum。 Here is my urls.py.这是我的 urls.py。

from django.urls import reverse
from django.urls import resolve
from django.test import TestCase
from .views import home,inspection_defects
from .models import Inspection

# Create your tests here.

class HomeTests(TestCase):
    def test_home_view_status_code(self):
        url = reverse('home')
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)

    def test_home_url_resolves_home_view(self):
        view = resolve('/')
        self.assertEquals(view.func, home)

class InspectionDefectTest(TestCase):
    def setUp(self):
        Inspection.objects.create(workOrderNum='312456',partNum='SD33345100-AQ1',customerName='TERRA Inc.',qtyInspected=10,qtyRejected=2)

    def test_inspection_defects_view_success_status_code(self):
        url = reverse('inspection_defects', kwargs={'pk': 1})
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)
    

Here is my urls.py file.这是我的 urls.py 文件。

from django.conf.urls import url
from django.contrib import admin

from finalreports import views

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^inspections/(?P<pk>\d+)/$', views.inspection_defects, name='inspection_defects'),
    url(r'^admin/', admin.site.urls),
]

Here's views.py这是views.py

from django.shortcuts import render
from .models import Inspection

# Create your views here.

def home(request):

    inspections = Inspection.objects.all()
    return render(request, 'home.html', {'inspections': inspections})

def inspection_defects(request, pk):
    inspection = Inspection.objects.get(pk=pk)
    return render(request, 'topics.html', {'inspection': inspection})

Here's topics.html这是主题。html

{% load static %}<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{{ inspection.workOrderNum }}</title>
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
  </head>
  <body>
    <div class="container">
      <ol class="breadcrumb my-4">
        <li class="breadcrumb-item">Inspections</li>
        <li class="breadcrumb-item active">{{ inspection.workOrderNum }}</li>
      </ol>
    </div>
  </body>
</html>

For some reason, Django is complaining that the test.py is failing due to an unexpected keyword argument partNum.出于某种原因,Django 抱怨 test.py 由于意外的关键字参数 partNum 而失败。

I don't know where to look for the problem.我不知道在哪里寻找问题。 It's getting frustrated thinking about programming all the time.一直在思考编程变得越来越沮丧。

The error is Inspection() got an unexpected keyword argument partNum and I don't know where to look for the error.错误是 Inspection() 有一个意外的关键字参数 partNum,我不知道在哪里查找错误。

In tests.py play with this instead.在 tests.py 中使用这个来代替。

class InspectionTests(TestCase):

    def setUp(self):
        Inspection.objects.create(workOrderNum='123DER', partNumber='sd', customerName="RYANTRONICS", qtyInspected=10, qtyRejected=3)

    def test_board_topics_view_success_status_code(self):
        url = reverse('inspection_topics', kwargs={'pk': 1})
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)

It's good to keep trying and take your time.继续努力并慢慢来是件好事。 After all, a fun project should not be rushed.毕竟,一个有趣的项目不应该匆忙。

The hardest thing in Django is getting the URLs to work. Django 中最难的事情是让 URL 正常工作。

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

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