如何测试在 Django 中有外键的模型?
how to test a model that has a foreign key in django?
我正在使用 python 3.5 和 Django 1.10 并尝试在 tests.py 中测试我的应用程序,但出现错误,它说:ValueError: Cannot assign "1": "NewsLetter.UserID" must be a "User" instance.
那么如何测试 fk这里的价值?
这是代码:
class NewsletterModelTest(TestCase):
@classmethod
def setUpTestData(cls):
#Set up non-modified objects used by all test methods
NewsLetter.objects.create(NewsLetterID=1, Email='test@test.com', Connected=False,UserID=1)
class NewsLetter(models.Model):
NewsLetterID = models.AutoField(primary_key=True)
Email = models.CharField(max_length=255)
Connected = models.BooleanField(default=False)
UserID = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
db_table = 'NewsLetter'
在您的 setupTestData 方法中,您必须创建一个 User 对象,并将其传递给 NewsLetter 对象的创建方法。
@classmethod
def setUpTestData(cls):
#Set up non-modified objects used by all test methods
user = User.objects.create(<fill params here>)
NewsLetter.objects.create(NewsLetterID=1, Email='test@test.com', Connected=False,UserID=user)
致降落在这里的人。
要为具有 ForeignKey 字段的模型编写测试,您需要创建 ForeignKey 指向的模型实例 ,然后在 ForeignKey 实例上调用 save(),然后将其应用于创建测试目标模型。
例如。 (为简洁起见进行了简化)
class BookTestCase(TestCase):
def test_fields_author_name(self):
author = Author(name="Mazuki Sekida")
author.save()
book = Book(name="Zen Training", author=author)
book.save()
# assertion example ...
record = Book.objects.get(id=1)
self.assertEqual(record.author.name, "Mazuki Sekida")
与@Arpit Solanki 的回答非常相似,这是我所做的:
from datetime import date
from django.test import TestCase
from ..models import Post, Author
class PostModelTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.author_ = 'Rambo'
cls.author = Author.objects.create(name=cls.author_)
cls.post = Post.objects.create(
title='A test', author=cls.author, content='This is a test.', date=date(2021, 6, 16))
def test_if_post_has_required_author(self):
self.assertEqual(self.post.author.name, self.author_)
我正在使用 python 3.5 和 Django 1.10 并尝试在 tests.py 中测试我的应用程序,但出现错误,它说:ValueError: Cannot assign "1": "NewsLetter.UserID" must be a "User" instance.
那么如何测试 fk这里的价值?
这是代码:
class NewsletterModelTest(TestCase):
@classmethod
def setUpTestData(cls):
#Set up non-modified objects used by all test methods
NewsLetter.objects.create(NewsLetterID=1, Email='test@test.com', Connected=False,UserID=1)
class NewsLetter(models.Model):
NewsLetterID = models.AutoField(primary_key=True)
Email = models.CharField(max_length=255)
Connected = models.BooleanField(default=False)
UserID = models.ForeignKey(User, on_delete=models.CASCADE)
class Meta:
db_table = 'NewsLetter'
在您的 setupTestData 方法中,您必须创建一个 User 对象,并将其传递给 NewsLetter 对象的创建方法。
@classmethod
def setUpTestData(cls):
#Set up non-modified objects used by all test methods
user = User.objects.create(<fill params here>)
NewsLetter.objects.create(NewsLetterID=1, Email='test@test.com', Connected=False,UserID=user)
致降落在这里的人。
要为具有 ForeignKey 字段的模型编写测试,您需要创建 ForeignKey 指向的模型实例 ,然后在 ForeignKey 实例上调用 save(),然后将其应用于创建测试目标模型。
例如。 (为简洁起见进行了简化)
class BookTestCase(TestCase):
def test_fields_author_name(self):
author = Author(name="Mazuki Sekida")
author.save()
book = Book(name="Zen Training", author=author)
book.save()
# assertion example ...
record = Book.objects.get(id=1)
self.assertEqual(record.author.name, "Mazuki Sekida")
与@Arpit Solanki 的回答非常相似,这是我所做的:
from datetime import date
from django.test import TestCase
from ..models import Post, Author
class PostModelTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.author_ = 'Rambo'
cls.author = Author.objects.create(name=cls.author_)
cls.post = Post.objects.create(
title='A test', author=cls.author, content='This is a test.', date=date(2021, 6, 16))
def test_if_post_has_required_author(self):
self.assertEqual(self.post.author.name, self.author_)