2
我在寫一些測試來檢查我的基本博客應用程序的模型。該模型要求博客標題是唯一的。以下是我已經寫了節省2篇博客文章測試的身體:Django:兩個字段是唯一的,但仍然失敗UNIQUE約束
first_post.title = "First Post!"
first_post.body = "This is the body of the first post"
first_post.pub_date = datetime.date.today()
first_post.tags = all_tags[0]
first_post.slug = "first_post"
first_post.save()
second_post = Post()
second_post.title = "Second Post!"
self.assertNotEqual(first_post.title,second_post.title)
second_post.body = "This is the body of the Second post"
second_post.pub_date = datetime.date.today()
second_post.tags = all_tags[1]
second_post.slug = "second"
second_post.save()
注意self.assertNotEqual(first_post.title, second_post.title)
。我加了這個,因爲當我運行測試時,我總是收到django.db.utils.IntegrityError: UNIQUE constraint failed: blog_post.title_text
。當我通過吐出的這個吐出口的剩餘部分完成時,它指向second_post.save()
。但是,assertNotEqual
總是通過,如果我將其更改爲assertEqual
,則會失敗。
無論我把什麼放入標題值,我都會得到相同的錯誤。爲什麼這兩個Post對象被認爲具有相同的標題?
僅供參考,這裏是博客模式:
class Post(models.Model):
title_text = models.CharField(max_length = 200, unique = True)
pub_date = models.DateTimeField('date published')
post_tags = models.ManyToManyField('Tag')
post_body = models.TextField()
slug = models.SlugField(max_length = 50, unique = True)
人,需要更多的咖啡。好地方!謝謝。一旦時間限制讓我接受,我會接受。 – MKreegs