2017-07-02 43 views
0

我想通過使用APIClient類而不是APIRequestFactory對我新創建的視圖集進行簡單測試,但視圖失敗,並顯示以下錯誤:Django Rest Framework AttributeError:'響應'對象沒有'編碼'屬性

AttributeError: 'Response' object has no attribute 'encoding' 

如果我使用API​​RequestFactory雖然,但我因爲我使用TokenAuthentication需要APIClient它工作正常。

這是上下文中,我使用的客戶端:

api_client = APIClient() 
reversed_url = reverse('showcase:ajax:publisher-dashboard', kwargs={'pk': 1}) 
client_token = cm.Client.objects.get(first_name='Cliente 1', 
            last_name='Prueba Showcase').user.auth_token.key 
api_client.credentials(HTTP_AUTHORIZATION='Token ' + client_token) 

request = api_client.get(reversed_url, format='json') 
response = views.PubViewSet.as_view({'get': 'dashboard'})(request, pk=1) 

的結果是不提供證書和settings.py中禁用TokenAuth相同。

<Response status_code=200, "text/html; charset=utf-8"> 

_charset = {NoneType} None 
_closable_objects = {list} <class 'list'>: [] 
_container = {list} <class 'list'>: [b''] 
_content_type_for_repr = {str} ', "text/html; charset=utf-8"' 
_handler_class = {NoneType} None 
_headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')} 
_is_rendered = {bool} False 
_post_render_callbacks = {list} <class 'list'>: [] 
_reason_phrase = {NoneType} None 
_request = {NoneType} None 
charset = {str} 'utf-8' 
closed = {bool} False 
content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag 
content_type = {NoneType} None 
context_data = {NoneType} None 
cookies = {SimpleCookie} 
data = {dict} {'publisher': {'id': 1, 'first_name': '', 'last_name': '', 'name': 'TEST', 'official_name': 'TEST, S.A.', 'rfc': 'XAXXXXXXX000X', 'street': 'St.', 'number': '420', 'interior': '', 'neighborhood': '', 'zipcode': '', 'email': '', 'phone': '', 'image': '/rcro 
exception = {bool} False 
is_rendered = {bool} False 
reason_phrase = {str} 'OK' 
rendered_content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag 
rendering_attrs = {list} <class 'list'>: ['template_name', 'context_data', '_post_render_callbacks'] 
status_code = {int} 200 
status_text = {str} 'OK' 
streaming = {bool} False 
template_name = {NoneType} None 
using = {NoneType} None 

使用時和APIClient:

這些使用API​​RequestFactory當是響應對象的內容

<Response status_code=200, "text/html; charset=utf-8"> 

_charset = {NoneType} None 
_closable_objects = {list} <class 'list'>: [] 
_container = {list} <class 'list'>: [b''] 
_content_type_for_repr = {str} ', "text/html; charset=utf-8"' 
_handler_class = {NoneType} None 
_headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')} 
_is_rendered = {bool} False 
_post_render_callbacks = {list} <class 'list'>: [] 
_reason_phrase = {NoneType} None 
_request = {NoneType} None 
charset = {str} 'utf-8' 
closed = {bool} False 
content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag 
content_type = {NoneType} None 
context_data = {NoneType} None 
cookies = {SimpleCookie} 
data = {dict} {'publisher': {'id': 1, 'first_name': '', 'last_name': '', 'name': 'TEST', 'official_name': 'TEST, S.A.', 'rfc': 'XAXXXXXXX000X', 'street': 'St.', 'number': '420', 'interior': '', 'neighborhood': '', 'zipcode': '', 'email': '', 'phone': '', 'image': '/rcro 
exception = {bool} False 
is_rendered = {bool} False 
reason_phrase = {str} 'OK' 
rendered_content = {str} 'Traceback (most recent call last):\n File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n attr = getattr(var, n)\n File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag 
rendering_attrs = {list} <class 'list'>: ['template_name', 'context_data', '_post_render_callbacks'] 
status_code = {int} 200 
status_text = {str} 'OK' 
streaming = {bool} False 
template_name = {NoneType} None 
using = {NoneType} None 

回答

0

我覺得你在這行代碼有錯誤:

request = api_client.get(reversed_url, format='json') 
response = views.PubViewSet.as_view({'get': 'dashboard'})(request, pk=1) 

api_client.get已經返回HTTPResponse,所以你不需要傳遞我t以VewSet作爲請求。相反,它試試這個:

response = api_client.get(reversed_url) 
assert response.status_code == 200 
+1

你是完全正確的,我認爲這是在替代APIRequestFactory下降,但它實際上產生一個完整的GET請求到指定的URL。我不得不再次看看[Django的原始客戶端類](https://docs.djangoproject.com/en/1.11/topics/testing/tools/#the-test-client)來驗證這一點。謝謝你澄清這一點。 – arielnmz

相關問題