python - self.assertContains 失败;在响应中找不到 *word*
问题描述
我很难完全渲染TopicsPage
. 假设渲染sub_heading.html
哪个扩展listing.html
(两个模板都位于同一个模板文件夹中)。测试通过了self.assertTemplateUsed()
断言。
然而,在以下情况下会引发 AssertionError:
self.assertContains(response, "Looking for more?")
AssertionError: False is not true : Couldn't find 'Looking for more?' in response
sub_heading.html
当模板已经被使用时,如何获取要渲染的内容以通过测试?我故意将 GET 方法的实现pass
只是为了展示我如何对视图进行子类化。
test_views.py
class TestTopicsPage__002(TestCase):
'''Verify that a topic and associated information is displayed
on the main page once the topic is created'''
@classmethod
def setUpTestData(cls):
user = User.objects.create_user("TestUser")
python_tag = Tag.objects.create(name="Python")
js_tag = Tag.objects.create(name="JavaScript")
content = {
'heading': "Test Title",
'text': "Mock text content",
'posted': datetime.datetime.now()
}
cls.topic = Topic(**content)
cls.topic.author = user
cls.topic.save()
cls.topic.tags.add(python_tag, js_tag)
def test_topics_main_page_rendered_topics(self):
response = self.client.get(
reverse("listing")
)
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "topics/sub_listing.html")
self.assertContains(response, "Looking for more?")
视图.py
class AbstractListingPage(TemplateView):
template_name = "topics/sub_listing.html"
extra_content = {
'query_links': ['interesting', 'hot', 'week', 'month']
}
def get_context_data(self):
context = super().get_context_data()
context['topics'] = Topic.objects.all()
context['search_form'] = SearchForm()
context['login_form'] = UserLoginForm
return context
def post(self, request):
context = self.get_context_data()
form = context['login_form'](data=request.POST)
if form.is_valid():
resolver = resolve(request.path)
login(request, form.get_user())
if resolver.namespace:
url = f"{resolver.namespace}:{resolver.url_name}"
else:
url = resolver.url_name
return HttpResponseRedirect(
reverse(url)
)
return self.render_to_response(context)
class TopicsPage(AbstractListingPage):
def get(self, request):
pass
清单.html
{% extends 'index.html' %}
{% load static %}
{% block content %}
{% if not topics %}
<h1 class="topics_placeholder">"Whoops...no topics are being talked about"</h1>
<h2>Join the community...NOW!</h2>
{% else %}
{% for topic in topics %}
<ul class="topic_stats">
<li>{{ topic.answers.count }} answers</li>
<li>{{ topic.likes }} likes</li>
<li>{{ topic.views }} views</li>
</ul>
<div class="topic_wrapper">
<h1><a href="{% url 'topics:topic' id=topic.id %}">{{ topic }}</a></h1>
<ul>
{% for tag in topic.tags.all %}
<li><a href="{% url 'topics:tag_listing' tag=tag %}">{{ tag }}</a></li>
{% endfor %}
</ul>
<p>{{ topic.posted }}</p>
<p><a href="{% url 'users:profile' id=topic.author.id %}">{{ topic.author }}</a></P>
</div>
{% endfor %}
{% endif %}
{% endblock content %}
{% block page_branch %}
{% endblock %}
<div class="question-header">
<button class="flex_item_btn button_widget red" type="button">
<a href="{% url 'topics:ask' %}">Ask Question</a>
</button>
</div>
sub_listing.html
{% extends 'topics/listing.html' %}
{% load static %}
{% block page_branch %}
<h2>Looking for more? Browse the <a href="{% url 'topics:paginated' %}">complete list of questions</a>
or <a href="{% url 'topics:tag_listing' %}">popular tags</a></h2>
{% endblock %}
解决方案
Listing.html 底部的内容是孤立的,不存在于父模板中的块中。
{% block page_branch %}
{% endblock %}
<div class="question-header">
<button class="flex_item_btn button_widget red" type="button">
<a href="{% url 'topics:ask' %}">Ask Question</a>
</button>
</div>
由于listing.html 扩展了index.html,它只能覆盖index.html 中存在的块。上述内容必须放在 {% block content %} 内才能完全呈现。然后 sub_listing.html 对 {% block page_branch %} 的使用将被渲染。
推荐阅读
- ruby-on-rails - 哈希数组每个在特定值时做什么?
- reactjs - React Native - Jest:自更新到 0.56 后已损坏。如何解决?
- android - 倒数计时器在android服务中不起作用
- mysql - sql更新表为null
- c++ - QtWidget 窗口的 QQuickView 子窗口
- android - 自动恢复在android中播放的背景音乐
- c - 在 C/C++ 中在 Raspberry Pi 中播放 WAV,必须使用 dsp?
- java - SpringMVC 表单中的两个按钮
- python - 用整数替换字典中的键
- flutter - Flutter 以编程方式关闭应用程序但从 appState