I have a function which i call from a unittest. From setting some debug traces i know the function worked like a charm and has all the values correctly prepared for return.
我有一个我从单元测试中调用的函数。从设置一些调试跟踪,我知道该功能像魅力一样工作,并具有正确准备返回的所有值。
This is what my testcode looks like (see where my ipdb.set_trace() is ):
这就是我的testcode的样子(请参阅我的ipdb.set_trace()所在的位置):
@override_settings(REGISTRATION_OPEN=True)
def test_confirm_account(self):
""" view that let's a user confirm account creation and username
when loggin in with social_auth """
request = self.factory.get('')
request.user = AnonymousUser()
request.session={}
request.session.update({self.pipename:{'backend':'facebook',
'kwargs':{'username':'Chuck Norris','response':{'id':1}}}})
# this is the function of which i need the context:
response = confirm_account(request)
self.assertEqual(response.context['keytotest'],'valuetotest')
From what i know from this part of the Django docs, i would be able to access response.context when i have used the testing client. But when i try to access response.context like i did it, i get this:
根据我从Django文档的这一部分所知,当我使用测试客户端时,我将能够访问response.context。但是当我尝试访问response.context就像我做的那样,我得到了这个:
AttributeError: 'HttpResponse' object has no attribute 'context'
AttributeError:'HttpResponse'对象没有属性'context'
Is there a way to get the special HttpResponse object of the client, without using the client?
有没有办法在不使用客户端的情况下获取客户端的特殊HttpResponse对象?
5 个解决方案
#1
9
The RequestFactory does not touch the Django middleware, and as such, you will not generate a context (i.e. no ContextManager middleware).
RequestFactory不会触及Django中间件,因此,您不会生成上下文(即没有ContextManager中间件)。
If you want to test the context, you should use the test client. You can still manipulate the construction of the request in the test client either using mock or simply saving your session ahead of time in the test, such as:
如果要测试上下文,则应使用测试客户端。您仍然可以使用模拟操作测试客户端中的请求构造,或者只是在测试中提前保存会话,例如:
from django.test import Client
c = Client()
session = c.session
session['backend'] = 'facebook'
session['kwargs'] = {'username':'Chuck Norris','response':{'id':1}}
session.save()
Now when you load the view with the test client, you'll be using the session as you set it, and when you use response = c.get('/yourURL/')
, you can then reference the response context using response.context
as desired.
现在,当您使用测试客户端加载视图时,您将在设置时使用会话,并且当您使用response = c.get('/ yourURL /')时,您可以使用响应来引用响应上下文。根据需要的背景。
#2
1
Though this is an old post, I suppose this tip can be of help. You can look into using TemplateResponse
(or SimpleTemplateResponse
) which can be substituted for render
or render_to_response
.
虽然这是一个老帖子,但我认为这个提示可以提供帮助。您可以查看使用TemplateResponse(或SimpleTemplateResponse),它可以替换render或render_to_response。
The Django docs has more on this
Django文档对此有更多的了解
#3
0
Yes, you can. You have to patch render.
是的你可以。你必须补丁渲染。
I'm using pytest-django
我正在使用pytest-django
class Test:
def context(self, call_args):
args, kwargs = call_args
request_mock, template, context = args
return context
@patch('myapplication.views.render')
def test_(self, mock_render, rf):
request = rf.get('fake-url')
view(request)
context = self.context(mock_render.call_args)
keytotest = 'crch'
assert keytotest == context['keytotest']
#4
0
The "response.context" is incorrect but you can use response.context_data to get the same context that passed to TemplateResponse.
“response.context”不正确,但您可以使用response.context_data来获取传递给TemplateResponse的相同上下文。
#5
-3
context (sic!) can be found in Response class. As you see it says it's HTTPResponse you get back from the view function. This happened because you've called it directly. Call this function via test client and it will be okay.
context(sic!)可以在Response类中找到。如您所见,它是HTTPResponse,您从视图功能返回。发生这种情况是因为您直接调用了它。通过测试客户端调用此函数,它会没问题。
response = client.get('/fobarbaz/')
response.context
#1
9
The RequestFactory does not touch the Django middleware, and as such, you will not generate a context (i.e. no ContextManager middleware).
RequestFactory不会触及Django中间件,因此,您不会生成上下文(即没有ContextManager中间件)。
If you want to test the context, you should use the test client. You can still manipulate the construction of the request in the test client either using mock or simply saving your session ahead of time in the test, such as:
如果要测试上下文,则应使用测试客户端。您仍然可以使用模拟操作测试客户端中的请求构造,或者只是在测试中提前保存会话,例如:
from django.test import Client
c = Client()
session = c.session
session['backend'] = 'facebook'
session['kwargs'] = {'username':'Chuck Norris','response':{'id':1}}
session.save()
Now when you load the view with the test client, you'll be using the session as you set it, and when you use response = c.get('/yourURL/')
, you can then reference the response context using response.context
as desired.
现在,当您使用测试客户端加载视图时,您将在设置时使用会话,并且当您使用response = c.get('/ yourURL /')时,您可以使用响应来引用响应上下文。根据需要的背景。
#2
1
Though this is an old post, I suppose this tip can be of help. You can look into using TemplateResponse
(or SimpleTemplateResponse
) which can be substituted for render
or render_to_response
.
虽然这是一个老帖子,但我认为这个提示可以提供帮助。您可以查看使用TemplateResponse(或SimpleTemplateResponse),它可以替换render或render_to_response。
The Django docs has more on this
Django文档对此有更多的了解
#3
0
Yes, you can. You have to patch render.
是的你可以。你必须补丁渲染。
I'm using pytest-django
我正在使用pytest-django
class Test:
def context(self, call_args):
args, kwargs = call_args
request_mock, template, context = args
return context
@patch('myapplication.views.render')
def test_(self, mock_render, rf):
request = rf.get('fake-url')
view(request)
context = self.context(mock_render.call_args)
keytotest = 'crch'
assert keytotest == context['keytotest']
#4
0
The "response.context" is incorrect but you can use response.context_data to get the same context that passed to TemplateResponse.
“response.context”不正确,但您可以使用response.context_data来获取传递给TemplateResponse的相同上下文。
#5
-3
context (sic!) can be found in Response class. As you see it says it's HTTPResponse you get back from the view function. This happened because you've called it directly. Call this function via test client and it will be okay.
context(sic!)可以在Response类中找到。如您所见,它是HTTPResponse,您从视图功能返回。发生这种情况是因为您直接调用了它。通过测试客户端调用此函数,它会没问题。
response = client.get('/fobarbaz/')
response.context