gpt4 book ai didi

python - self.assertTrue(response.content 中的 post.text) – 断言错误

转载 作者:行者123 更新时间:2023-11-30 23:25:21 25 4
gpt4 key购买 nike

此行失败:

self.assertTrue(response.content 中的 post.text)

测试.py
from django.test import TestCase, LiveServerTestCase, Client
from django.utils import timezone
from blogengine.models import Post

class PostViewTest(LiveServerTestCase):

def setUp(self):
self.client = Client()

def test_single(self):
# Create the post
post = Post()
post.title = 'My first post'
post.slug = 'my-first-post'
post.excerpt = 'This is the excerpt'
post.text = 'This is my first blog post'
post.pub_date = timezone.now()
post.tags = 'test-tag'
post.save()

# Check new post saved
all_posts = Post.objects.all()
self.assertEquals(len(all_posts), 1)

# Fetch the single post
response = self.client.get('/blog/?P<slug>[\w\-]+)/')
self.assertEquals(response.status_code, 200)

# Check the post title is in the response
self.assertTrue(post.title in response.content)

# Check that the slug is correct
self.assertTrue(post.slug in response.content)

# Check the text is in the response
self.assertTrue(post.text in response.content)

# Check the post date is in the response
self.assertTrue(str(post.pub_date.year) in response.content)
self.assertTrue(post.pub_date.strftime('%b') in response.content)
self.assertTrue(str(post.pub_date.day) in response.content)

# Check the post tags are in the response
self.assertTrue(post.tags in response.content)

single.html

{% extends 'base.html' %}

{% load markdown_deux_tags %}

{% block title %}The Blog of David Ingledow – {{ post.title }}{% endblock %}

{% block content %}
<h1>{{ post.title }}</h1>
<p>
<i>{{ post.pub_date }}</i>
</p>
{{ post.text | markdown }}
{{ post.tags }}
{% endblock %}

模型.py
from django.db import models
from django.core.urlresolvers import reverse

# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True, max_length=255, blank=True)
pub_date = models.DateTimeField()
excerpt = models.TextField(blank=True)
text = models.TextField()
tags = models.CharField(max_length=80, blank=True)
published = models.BooleanField(default=True)

def __unicode__(self):
return u'%s' % self.title

def get_absolute_url(self):
return reverse('blogengine.views.post', args=[self.slug])

views.py
from django.shortcuts import render, get_object_or_404
from blogengine.models import Post

# Create the post list view (david.ingledow.co.uk/blog)
def index(request):
# get the blog posts that are published
posts = Post.objects.filter(published=True).order_by('-pub_date')
# now return the rendered template
return render(request, 'blogengine/index.html', {'post': posts})

def post(request, slug):
# get the Post object
post = get_object_or_404(Post, slug=slug)
# now return the rendered template
return render(request, 'blogengine/single.html', {'post': post})

blogengine urls.py
from django.conf.urls import patterns, url
from django.views.generic import ListView
from blogengine.models import Post

urlpatterns = patterns('',
url(r'^$', 'blogengine.views.index'),
url(r'^(?P<slug>[\w\-]+)/$', 'blogengine.views.post'),
)

最佳答案

测试中的这一行没有任何意义:

response = self.client.get('/blog/?P<slug>[\w\-]+)/')

您不使用正则表达式来访问 URL:您使用的是 URL。在这种情况下,您需要使用您尝试访问的实际博客的别名:

response = self.client.get('/blog/my-first-slug/')

关于python - self.assertTrue(response.content 中的 post.text) – 断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23036525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com