在tests.py中测试HomeTests类在第29行出现错误 [英] Testing the HomeTests class in tests.py has an error in line 29
问题描述
我正在继续我正在做的检查/缺陷"项目.在第29行,该错误表明HomeTests没有属性Inspection,我正在尝试找出原因.
I am continuing the Inspections/Defects project that I am doing for fun. On line 29, the error says that HomeTests has no attribute Inspection, and I am trying to find out why.
这是tests.py文件.
Here is the tests.py file.
class HomeTests(TestCase):
def setUp(self):
Inspection.objects.create(workOrderNum='123DER', partNumber='sd', customerName="RYANTRONICS", qtyInspected=10,qtyRejected=3)
url = reverse('home')
self.response = self.client.get(url)
def test_home_view_status_code(self):
self.assertEquals(self.response.status_code, 200)
def test_home_url_resolves_home_view(self):
view = resolve('/')
self.assertEquals(view.func, home)
def test_home_url_resolves_home_view(self):
view = resolve('/')
self.assertEquals(view.func, home)
def test_home_view_contains_link_to_topics_page(self):
inspection_topics_url = reverse(inspection_topics, kwargs={'pk': self.inspection.pk}) -> error occurs at this line
self.assertContains(self.response, 'href="{0}"'.format(inspection_topics_url))
我也一直在查看views.py文件,看是否是问题的根源.我在
I also have been looking in my views.py file to see if it was the source of the problem. I followed the guide at Django URLs Advanced but I had missed something. Here is the views.py file in case you wanted to look at it.
from django.shortcuts import render,get_object_or_404
from .models import Inspection
# Create your views here.
def home(request):
inspections = Inspection.objects.all()
return render(request, 'home.html', {'inspections': inspections})
def inspection_topics(request, pk):
inspection = get_object_or_404(Inspection, pk=pk)
return render(request, 'topics.html', {'inspection': inspection})
views.py有两个文件,home.html和topic.html.他们在这里.
The views.py has two files, home.html and topics.html. Here they are.
{% load static %}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Final Inspection Report</title>
<link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
</head>
<body>
<div class="container">
<ol class="breadcrumb my-4">
<li class="breadcrumb-item active">Final Inspection Report</li>
</ol>
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Inspection</th>
<th>Customer Name</th>
<th>Quanity Inspected</th>
<th>Quantity Rejected</th>
</tr>
</thead>
<tbody>
{% for inspection in inspections %}
<tr>
<td>
{{ inspection.workOrderNum }}
<small class="text-muted d-block">{{ inspection.partNumber }}</small>
</td>
<td class="align-middle">{{ inspection.customerName }}</td>
<td class="align-middle">{{ inspection.qtyInspected }}</td>
<td class="align-middle">
{{ inspection.qtyRejected }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
这是存储检查的主页.
这是topic.html.
And here is topics.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>
当我这样做时,我得到的错误是HomeTests没有属性检查,但是我应该得到的错误是False,这不是真的:无法找到href =/inspections/1/.如何修复HomeTests没有属性错误?
When I did this, the error I got was HomeTests has no attribute inspection, but the error that I should have gotten was False is not true: Couldn't find href=/inspections/1/. How do I fix the HomeTests has no attribute error?
推荐答案
请改用此:
self.inspection = Inspection.objects.create(
workOrderNum='123DER',
partNumber='sd',
customerName='RYANTRONICS',
qtyInspected=10,
qtyRejected=3,
)
通常,将来的 tests.py
:
self.[model] = [model].object.create(attributes_of_model)
其中[model]是models.py文件中模型类的名称,attributes_of_model是模型具有的属性.
where [model] is the name of your model class in the models.py file and attributes_of_model are the attribute(s) that model has.
这篇关于在tests.py中测试HomeTests类在第29行出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!