[TIP] Unable to mock.patch methods in __init__.py
alfredodeza at gmail.com
Sat Jul 14 13:21:07 PDT 2012
On Sat, Jul 14, 2012 at 3:48 PM, Ratnadeep Debnath <rtnpro at transifex.com>wrote:
> I am trying to patch gettext(), ugettext(), etc. methods in
> django/utils/translation/__init__.py (django.utils.translation). I am
> trying something like below:
> from mock import patch
> def mock_gettext(s):
> # do something
> @patch('django.utils.translation.gettext', mock_gettext)
> def myview(request):
> This is just not working. I see no reason why it should not. I
> successfully patched `_trans` object from the same module above.
That will not work as you are patching it in the wrong namespace. What you
need to do here is to patch in
the namespace of your view.
So if your view is in the package: my_package.tests.test_my_view and in
there you are importing gettext from django.utils.translation
then what you need to patch it like this:
Also, from your code example it doesn't seem like you where trying to patch
on a test is this correct?
Regardless, patching on the right namespace as explained above should get
you some progress.
> Can anyone help me out?
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the testing-in-python