库文件
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)
测试.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())
print mock_reverse.mock_calls # prints []
print mock_reverse.mock_calls # prints []
self.assertTrue('/natrium/cb/details/' in response)
但相反,我得到
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.
为什么它调用reverse
而不是我的mock_reverse
(它正在查找我的 urls.py !!)你可以看到calls
返回一个空列表。我什assert mock_reverse.called
至它返回错误。
也许我没有提到这个,但是lib只是一堆函数,没有类。