python - mocking django settings: AttributeError: 'Settings' object has no attribute 'FOO' -
python - mocking django settings: AttributeError: 'Settings' object has no attribute 'FOO' -
attributes on settings
disapear after using this:
.... here settings.foo exist. mock.patch('django.conf.settings.foo', 123, create=true): ... ... here settings.foo gone.
why happen?
i found old bug, can't believe still alive, since bug 4 years old:
http://code.google.com/p/mock/issues/detail?id=59
we utilize mock 1.0.1 pypi.
consider simple function:
testapp/views.py
:
from django.conf import settings def return_settings_foo(): homecoming settings.foo
then in shell:
in [9]: testapp import views in [10]: print views.return_settings_foo() test in [11]:
next mock settings.foo:
in [11]: mock.patch('testapp.views.settings.foo', 'mocked'): print views.return_settings_foo() ....: mocked
so, must mock settings module calling it, (not located) case testapp/views
.
test same:
import mock django.test import testcase testapp import views class testprintfoo(testcase): @mock.patch('testapp.views.settings.foo', 'mocked') def test_print(self): result = views.return_settings_foo() self.assertequal(result, 'mocked') def test_not_mocked_print(self): result = views.return_settings_foo() self.assertequal(result, 'test')
upd
one more thing. when utilize create=true
on attribute exists, no matter, if existed or not, deleted after context end in __exit__
, can utilize pdb
see that. foo attr deleted after context
> /usr/local/lib/python2.7/dist-packages/mock.py(1381)__exit__() 1380 else: -> 1381 delattr(self.target, self.attribute) 1382 if not self.create , not hasattr(self.target, self.attribute): ipdb> self.target, self.attribute (<django.conf.lazysettings object @ 0x7f48f067db10>, 'foo') ipdb>
python django unit-testing mocking django-settings
Comments
Post a Comment