14

代码:类 StockFactory(UniqueObjectsFactory): FACTORY_FOR = Stock FACTORY_DJANGO_GET_OR_CREATE = ('name', 'market')

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'

有什么线索吗?想法?我经常和工厂男孩一起工作,而且大多数时候它是一个很好的工具。但经过数小时的调试,我就是找不到问题

4

1 回答 1

33

我太愚蠢了:那些行是错误的:

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

本来应该:

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

希望它可以帮助遇到此问题的其他人

于 2014-03-04T07:35:55.747 回答