Sorry, I missed your reply. I do not have modules/db.py
I was attempting to mock the definition of 'ldap_auth' in models/db.py (It's not essential that the definition live there.) It looks like when the tests/sign_infra.py mock is applied for "db.ldap_auth", the web2py custom import hook gets involved and doesn't resolve models/db.py I'm not clear on the effective namespace of my web2py app, which I need to understand to define mocks. If there are *some* places where I can apply mocks and not others thats fine, I'd just like a little help working out where. I'm currently relying on full request testing (functional) which is broader than I would like and forced me to put an "if TEST_APP" predicate in production code. Again, that's ok but I'd like to have more granular tests. Cheers, Robin On Tuesday, 18 April 2017 11:50:36 UTC+1, Marlysson Silva wrote: > > Do you have a file called db in your modules folder in your application? > > Em domingo, 16 de abril de 2017 11:37:11 UTC-3, Robin Bryce escreveu: >> >> 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 >> >> -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

