Module import order influences whether patching works or not

Issue #17 new
cburgmer
created an issue

In testing my Django views I use the following lines to stub out access to a model: {{{ @fudge.patch('reversion.models.Version') def test_version_view(self, Version): version = construct_version() Version.has_attr(objects=fudge.Fake() .expects('get_for_object_reference') .returns(fudge.Fake() .expects('get') .with_args(id=version.id) .returns(version))) resp = self.client.get('/%s' % instance.pk, {'action': 'version', 'version_id': version.id}) }}} However, as the view is imported before the patching happens, the mock is not used. Rather than receiving a fake object reference the view still sees the model.

Delaying the import of the view fixes this issue. I would however expect Fudge to work in either condition.

The import in question is a {{{init.py}}} importing the view, which imports the affected model.

Comments (2)

  1. cburgmer reporter

    Seems to be something Django specific. Can't reproduce with the following code:

    >>> import fudge
    >>> def p(s):
    ...     print s
    ... 
    >>> import random
    >>> 
    >>> @fudge.patch('random.Random')
    ... def test1(fake_random):
    ...     fake_random.provides('__init__').provides('seed').calls(lambda: p("1st"))
    ...     random.Random().seed()
    ... 
    >>> test1()
    1st
    >>> 
    >>> 
    >>> 
    >>> @fudge.patch('random.Random')
    ... def test2(fake_random):
    ...     fake_random.provides('__init__').provides('seed').calls(lambda: p("2nd"))
    ...     random.Random().seed()
    ... 
    >>> test2()
    2nd
    
  2. Log in to comment