Django-工厂男孩在一家工厂没有明显原因失败

时间:2014-03-03 08:10:48

标签: django django-testing factory-boy

代码: class StockFactory(UniqueObjectsFactory):     FACTORY_FOR =股票     FACTORY_DJANGO_GET_OR_CREATE =('名称','市场')

market = factory.SubFactory(MarketFactory)
symbol = FuzzyAttribute(lambda: ''.join(random.choice(string.ascii_uppercase) for _ in xrange(4)))
name = FuzzyCompanyName()
# last_trade_price = fuzzy.FuzzyDecimal(0.0, 10000.0)


class PositionsFactory(FreezeableFactory):
    FACTORY_FOR = Position
    FACTORY_DJANGO_GET_OR_CREATE = ('stock','AnotherObject')

    id = FuzzyInteger(100000)
    stock = factory.SubFactory(Stock)

    AnotherObject = factory.SubFactory(AnotherObject)  #If I comment stock out it would fail here

    created_date = FuzzyDateTime(start_dt=datetime(2013, 1, 1, tzinfo=compat.UTC))

错误:

 File "/home/alon/Projects/stox-server/execution/tests/functional/test_positions.py", line 21, in setUp
    PositionsFactory.create( portfolio=self.portfolio)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 522, in create
    attrs = cls.attributes(create=True, extra=kwargs)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/base.py", line 365, in attributes
    force_sequence=force_sequence,
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 283, in build
    return stub.__fill__()
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 83, in __fill__
    res[attr] = getattr(self, attr)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
    val = val.evaluate(self, self.__containers)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
    containers=containers,
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 75, in evaluate
    return self.function(obj)
  File "/home/alon/Projects/stox-server/execution/tests/common/factories.py", line 173, in <lambda>
    symbol = factory.LazyAttribute(lambda pos: pos.stock.symbol)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 105, in __getattr__
    val = val.evaluate(self, self.__containers)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/containers.py", line 215, in evaluate
    containers=containers,
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 299, in evaluate
    return self.generate(sequence, obj, create, defaults)
  File "/home/alon/.virtualenvs/stox-server/local/lib/python2.7/site-packages/factory/declarations.py", line 386, in generate
    return subfactory.simple_generate(create, **params)
AttributeError: type object 'Stock' has no attribute 'simple_generate'

任何线索?想法?我和工厂男孩一起工作很多,而且大多数时候它都是一个很好的工具。但经过几个小时的调试后我才能找到问题

1 个答案:

答案 0 :(得分:30)

我这么傻: 那条线是错的:

stock = factory.SubFactory(Stock)
AnotherObject = factory.SubFactory(AnotherObject) 

应该是:

stock = factory.SubFactory(StockFactory)
AnotherObject = factory.SubFactory(AnotherObjectFactory) 

希望它可以帮助其他任何碰到这个问题的人