Hi,
I'm attempting to use the python mock library with some unittest based tests for a web2py application. It feels like I'm going against the grain.
General question: Is it generally a pain to use mock with web2py applications or am I missing some trick to get it working ?
Specifically, in models/db.py I have
from gluon.contrib.login_methods.ldap_auth import ldap_auth
in a unittest implementation I have
def mock_ldap_auth(*args,**kw):
return True
mock_ldap_auth = mock.patch(
'db.ldap_auth',
side_effect=mock_ldap_auth)
I'm using web2py_utils and webtest to drive unittest.
class TestSomething(unittest.TestCase):
# __init__ method which intialises webtest
def setUp(self):
p = mock_ldap_auth.start()
self.addCleanup(p.stop)
The call to 'mock_ldap_auth.start()' produces this traceback::
Traceback (most recent call last):
File "C:\Projects\web2py\applications\icss/tests\sign_infra.py", line 49, in setUp
p = mock_ldap_auth.start()
File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1500, in start
result = self.__enter__()
File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1353, in __enter__
self.target = self.getter()
File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1523, in <lambda>
getter = lambda: _importer(target)
File "C:\Projects\pyenvs\icss\lib\site-packages\mock\mock.py", line 1206, in _importer
thing = __import__(import_path)
File "C:\Projects\web2py\gluon\custom_import.py", line 89, in custom_importer
raise ImportError, 'Cannot import module %s' % str(e)
ImportError: Cannot import module 'applications.icss.modules.db'
Note: My purpose in asking this question is to avoid ugly and unnecessary case specific work arounds.
Thanks,
Robin