使用Factory Boy进行Django测试时出错

时间:2015-03-01 21:10:24

标签: python django unit-testing factory-boy

我在我的Django项目中使用Factory Boy。你能否向我解释一下我的错误:为什么我在运行' tests.py'时遇到错误? - ' ValueError:""需要有一个值的字段" post"在此之前可以使用多对多关系。'

这是我的代码:

import factory

from . import models

# factories


class TagFactory(factory.Factory):
    class Meta:
        model = models.Tag

    name = factory.Sequence(lambda n: 'tag-%s' % n)
    slug = factory.Sequence(lambda n: 'slug-%s' % n)


class CategoryFactory(factory.Factory):
    class Meta:
        model = models.Category

    name = factory.Sequence(lambda n: 'cat-%s' % n)
    slug = factory.Sequence(lambda n: 'cat-slug-%s' % n)


class PostFactory(factory.Factory):
    class Meta:
        model = models.Post

    title = factory.Sequence(lambda n: 'postik-%s' % n)
    text = 'some text'
    slug = factory.Sequence(lambda n: 'post_slug_%s' % n)
    category = factory.SubFactory(CategoryFactory)

    @factory.post_generation
    def tags(self, create, extracted, **kwargs):
        if not create:
            # Simple build, do nothing.
            return
        if extracted:
            for tag in extracted:
                self.tags.add(tag)

tests.py

from django.test import TestCase

from . import factories
from .models import Post, Category, Tag
from django.core.urlresolvers import reverse


class PostTests(TestCase):
    """
    display_post view test.
    """
    def setUp(self):
        self.tag = factories.TagFactory()
        self.category = factories.CategoryFactory()
        self.poster = factories.PostFactory.create(tags=('tag-1'))

 ...

models.py

from django.db import models
from django.core.urlresolvers import reverse
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from ckeditor.fields import RichTextField


class Category(models.Model):
    """
    Category.
    """

    name = models.CharField(max_length=20)
    slug = models.SlugField(max_length=20)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('categorier', args=[str(self.slug)])


class Tag(models.Model):
    """
    Tag.
    """

    name = models.CharField(max_length=100, unique=True)
    slug = models.SlugField(max_length=100, unique=True)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('tagger', args=[str(self.slug)])

    class Meta:
        verbose_name = _('Tag')


class Post(models.Model):
    """
    Post model.
    """

    title = models.CharField(max_length=150)
    created_date = models.DateTimeField(auto_now_add=True)
    image = models.ImageField(upload_to="pictures/%Y/%m/%d",
                                        blank=True, null=True)
    text = RichTextField(max_length=10000)
    slug = models.SlugField(max_length=150, unique=True)
    tags = models.ManyToManyField(Tag, null=True, blank=True)
    category = models.ForeignKey(Category)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('poster', args=[str(self.slug)])

    class Meta:
        ordering = ['-created_date']
        verbose_name = _('Eco Post')
        verbose_name_plural = _('Eco Posts')

回溯:

ERROR: test_display_post (posts.tests.PostTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/ecohata/posts/tests.py", line 17, in setUp
    self.poster = factories.PostFactory.create(tags=('tag-1'))
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/factory/base.py", line 585, in create
    return cls._generate(True, attrs)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/factory/base.py", line 516, in _generate
    results[name] = decl.call(obj, create, extraction_context)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/factory/declarations.py", line 490, in call
    extraction_context.value, **extraction_context.extra)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/ecohata/posts/factories.py", line 41, in tags
    self.tags.add(tag)
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/django/db/models/fields/related.py", line 1175, in __get__
    through=self.field.rel.through,
  File "/Users/boshepelinski/Documents/Projects/Django/ecohata/lib/python3.4/site-packages/django/db/models/fields/related.py", line 831, in __init__
    (instance, source_field_name))
ValueError: "<Post: postik-0>" needs to have a value for field "post" before this many-to-many relationship can be used.

1 个答案:

答案 0 :(得分:3)

修复非常简单:您的工厂继承自factory.Factory,但您使用的是Django模型。您应该继承factory.django.DjangoModelFactory

否则,factory_boy不知道您的对象生成后必须save(),之后您就会失败。

您的代码中还有另一个问题:当您撰写factories.PostFactory.create(tags=('tag-1'))时,这实际上与撰写factories.PostFactory.create(tags='tag-1)相同。

然而:

  • 您的@post_generation代码声明的定义需要一个标记列表,因此您应该使用factories.PostFactory.create(tags=['tag-1'])
  • 实际上,由于它调用了self.tags.add(tag),因此它需要一个可迭代的Tag个对象;它应该通过factories.PostFactory.create(tags=[TagFactory()])来调用。