2017-08-12 12 views
0

工場の少年を正しく使用することはできません。RecursionError:工場の少年を使用している場合

私の工場で:

import pytest 
from django.test import TestCase, client 
from harrispierce.factories import JournalFactory, SectionFactory 

@pytest.mark.django_db 
class TestIndex(TestCase): 

    @classmethod 
    def setUpTestData(cls): 
     cls.myclient = client.Client() 

    def test_index_view(self): 
     response = self.myclient.get('/') 
     assert response.status_code == 200 

    def test_index_content(self): 
     section0 = SectionFactory() 
     section1 = SectionFactory() 
     section2 = SectionFactory() 
     print('wijhdjk: ', section0) 
     journal1 = JournalFactory.create(sections=(section0, section1, section2)) 

     response = self.myclient.get('/') 

     print('wijhdjk: ', journal1) 
     self.assertEquals(journal1.name, 'Section0') 
     self.assertContains(response, journal1.name) 

しかしpytestを実行しているとき、私はこれを取得:

import factory 

from harrispierce.models import Article, Journal, Section 

class JournalFactory(factory.Factory): 
    class Meta: 
     model = Journal 

    name = factory.sequence(lambda n: 'Journal%d'%n) 

    @factory.post_generation 
    def sections(self, create, extracted, **kwargs): 
     if not create: 
      # Simple build, do nothing. 
      return 

     if extracted: 
      # A list of groups were passed in, use them 
      for section in extracted: 
       self.sections.add(section) 


class SectionFactory(factory.Factory): 
    class Meta: 
     model = Section 

    name = factory.sequence(lambda n: 'Section%d'%n) 

と私のテスト

journal1 = JournalFactory.create(sections=(section0, section1, section2))

harrispierce_tests/test_index.py:22:

RecursionError: maximum recursion depth exceeded while calling a Python object

!再帰が検出されました(同じ地方&の位置)

答えて

1

可能な問題は、適切なFactoryベースクラスを使用していないことです.Djangoモデルの場合は、factory.django.DjangoModelFactoryを使用します。

これで問題は発生しません。完全なスタックトレースが便利です。

@factory.post_generationセクションを削除して、適切なJournalオブジェクトがあるかどうかを確認してください。どのパラメータが通過したかを調べます。

コードを修正するだけでは不十分な場合は、factory_boy repositoryの問題を再現可能なテストケースで開くことをお勧めします(すでにsome branches/commitsが報告されているバグを再現しようとしています)。

関連する問題