0

I'd like to test function:

#foo_module.py
def foo(*args, **kwargs):
   bar = SomeClass(*args, **kwargs)
   # actions with bar
   del bar

I chose to test the mock library. My test looks like:

@mock.patch('path.to.foo_module.SomeClass')
def test_foo(self, mock_class):
    foo()
    mock_class.assert_called_once_with()

But how I can check that 'del bar' executed? Call of mock_class.return_value.__del__ raises AttributeError.

UPD: Sorry but I didn't mention that SomeClass is django.contrib.gis.gdal.datasource.DataSource. DataSource has overridden __del__ method:

def __del__(self):
    "Destroys this DataStructure object."
    if self._ptr and capi:
        capi.destroy_ds(self._ptr)

In this case del bar have effect outside the function. So I simple should to mock capi and check capi.destroy_ds.called.

ikoverdyaev
  • 783
  • 1
  • 12
  • 17

1 Answers1

-1

Check the dir() result to see that object is still there:

>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']
>>> a=10
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'a']
>>> del a
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__']

For this script:

def f() :
    a = 10
    print dir()
    del a
    print dir()
    objLst = dir()
    if 'a' not in objLst :
        print 'Object deleted'

f()

you get the output:

['a']
[]
Object deleted
Mihai Hangiu
  • 588
  • 4
  • 13