Hi Guys,
I'm using FactoryBoy in my tests, but I have a model which has a ManyToManyField to another model. Does anyone know if it's possible to make factoryboy use a factory to represent this relationship?
For reference, here are a sample model and factory:
--models.py
from django.contrib.auth.models import User
from django.db.models import ManyToManyField
class Workplace(models.Model):
employees = ManyToManyField(User)
--tests.py
import factory
from models import Workplace
# This will set the default strategy for all factories that don't define a default build strategy
factory.Factory.default_strategy = factory.BUILD_STRATEGY
class WorkplaceFactory(factory.Factory):
FACTORY_FOR = Workplace
class UserFactory(factory.Factory):
FACTORY_FOR = User
class ATestCase(TestCase):
def setUp(self):
super(ATestCase, self).setUp()
self.user = UserFactory.build()
self.workplace = WorkplaceFactory.build()
def test_foobar(self):
# how do I add self.user to self.workplace ?
self.assertEqual(self.workplace.employees_set.all(), 1)
--Craig