[TIP] Unable to mock.patch methods in __init__.py

Alfredo Deza alfredodeza at gmail.com
Sat Jul 14 13:21:07 PDT 2012


Hi Ratnadeep

On Sat, Jul 14, 2012 at 3:48 PM, Ratnadeep Debnath <rtnpro at transifex.com>wrote:

> Hi,
>
> 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:

@patch('my_package_name.tests.test_my_view.gettext', mock_gettext):
def test_my_view


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?
>
> Thanks,
> Regards,
> rtnpro
>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120714/71622ad9/attachment.html>


More information about the testing-in-python mailing list