質問

I am using Factory Boy in a Django project and I need to access class properties, defined in the class, in the corresponding factory.

My test case has something along the lines of:

from django.test.testcases import TransactionTestCase
from app.tests.factories import SomeModelFactory

class MyTestCase(TransactionTestCase):   
    def test_something(self):
        ...
        SomeModelFactory.create(
            data_type=SomeModelFactory.FACTORY_FOR.TYPE_STRING)
        ...

My class, as you may guess, has the corresponding constant in the right spot.

class SomeModel(models.Model):
  TYPE_STRING = 2
  ...

and the corresponding factory is defined as it should be

class SomeModelFactory(factory.Factory):
  FACTORY_FOR = SomeModel
  ...

The test case fails saying that FACTORY_FOR is not there. So, I think the metaclass is hidding that somewhere. Ideally, I would like to minimise the need to import the model directly if I am using the factory.

役に立ちましたか?

解決

SomeModelFactory._associated_class should be what you are looking for...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top