FactoryBoy / factory_boy

A test fixtures replacement for Python
https://factoryboy.readthedocs.io/
MIT License
3.53k stars 400 forks source link

SubFactory: weird behavior #263

Open spg opened 8 years ago

spg commented 8 years ago

In the snippet below, only test_stub fails. Is that expected?

from django.db import models
import factory
from factory.containers import StubObject

class B(models.Model):
    pass

class A(models.Model):
    b = models.ForeignKey(B)

class BFactory(factory.Factory):
    class Meta:
        model = B

class AFactory(factory.Factory):
    class Meta:
        model = A

    b = factory.SubFactory(BFactory)

def test_stub():
    a = AFactory.stub()
    assert isinstance(a, StubObject)
    assert isinstance(a.b, StubObject)  # this assertion fails. Is that per design?

def test_create():
    a = AFactory.create()
    assert isinstance(a, A)
    assert isinstance(a.b, B)

def test_build():
    a = AFactory.build()
    assert isinstance(a, A)
    assert isinstance(a.b, B)
rbarrois commented 8 years ago

Well, that's indeed a bug.

However, the "stub" strategy is less useful now that build() works properly, so improving "stub" support has not been high in the todo-list.

However, I'd be more than happy to merge a patch about this, and even to guide you in writing such a fix :)