我正在使用Mock,我无法正确修补django的render_to_response功能。例如,以下测试永远不会失败:
from django.test.client import Client
from mock import patch
import nose.tools as nt
@patch('django.shortcuts.render_to_response')
def test_should_fail(self, render_to_response):
def assert_response(url, context, context_instance):
nt.assert_false(True)
render_to_response.side_effect = assert_response
response = Client().get('/some/url/')
我做错了什么?
更新:我想这样做的原因是,在我看来,我正在呈现响应:
form = SomeFormClass(label_suffix='')
return render_to_response('admin/send_info_message.html', {'form': form,}, context_instance=RequestContext(request))
在我的测试中,我想测试这些参数是否被正确调用,如:
def assert_response(url, context, context_instance):
nt.assert_equal('admin/survey_question.html', url)
nt.assert_equal({'form': SomeForm()}, context)
nt.assert_equal(RequestContext(response.request), context_instance)
render_to_response.side_effect = assert_response
答案 0 :(得分:2)
首先,你为什么要这样做?
其次,您应该修补函数的位置是您调用它的位置 - 即在处理相关URL的视图模块中。例如:
@patch('myapp.views.render_to_response')