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

时间:2014-04-12 21:47:58

标签: python django django-models tdd django-views

这一行失败了:

self.assertTrue(post.text in response.content)

tests.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 %}

models.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'),
    )

1 个答案:

答案 0 :(得分:3)

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

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

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

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