Am I mocking this helper function right in my Django test?

Posted by CppLearner on Stack Overflow See other posts from Stack Overflow or by CppLearner
Published on 2012-04-10T08:57:08Z Indexed on 2012/04/12 17:29 UTC
Read the original article Hit count: 270

lib.py

from django.core.urlresolvers import reverse
def render_reverse(f, kwargs):
    """
    kwargs is a dictionary, usually of the form {'args': [cbid]}
    """
    return reverse(f, **kwargs)

tests.py

from lib import render_reverse, print_ls

class LibTest(unittest.TestCase):

def test_render_reverse_is_correct(self):
    #with patch('webclient.apps.codebundles.lib.reverse') as mock_reverse:
    with patch('django.core.urlresolvers.reverse') as mock_reverse:
        from lib import render_reverse
        mock_f = MagicMock(name='f', return_value='dummy_views')
        mock_kwargs = MagicMock(name='kwargs',return_value={'args':['123']})
        mock_reverse.return_value = '/natrium/cb/details/123'
        response = render_reverse(mock_f(), mock_kwargs())

    self.assertTrue('/natrium/cb/details/' in response)

But instead, I get

  File "/var/lib/graphyte-webclient/graphyte-webenv/lib/python2.6/site-packages/django/core/urlresolvers.py", line 296, in reverse
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'dummy_readfile' with arguments '('123',)' and keyword arguments '{}' not found.

Why is it calling reverse instead of my mock_reverse (it is looking up my urls.py!!)

The author of Mock library Michael Foord did a video cast here (around 9:17), and in the example he passed the mock object request to the view function index. Furthermore, he patched POll and assigned an expected return value.

Isn't that what I am doing here? I patched reverse?

Thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about django