I am using Mock and I am not able to patch django's render_to_response function properly. For e.g., the following test neve开发者_开发知识库r fails:
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/')
What am I doing wrong?
Update: The reason I want to do this is that in my view, I am rendering the response like:
form = SomeFormClass(label_suffix='')
return render_to_response('admin/send_info_message.html', {'form': form,}, context_instance=RequestContext(request))
In my test, I want to test that these parameters are correctly called, like:
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
Firstly, why do you want to do this?
Secondly, the place you should patch a function is the place you are calling it - ie in the views module that handles the relevant URL. For example:
@patch('myapp.views.render_to_response')
精彩评论