从基于类的通用视图手动获取响应

时间:2013-04-21 16:05:00

标签: python django django-class-based-views django-generic-views

我正在尝试编写一个测试来验证从基于类的通用视图返回的HTML。假设我有这个基于函数的视图,只需渲染一个模板:

# views.py
from django.shortcuts import render

def simple_view(request, template='template.html'):
    return render(request, template)

有了这个,在测试期间我可以这样做:

# tests.py
from django.http import HttpRequest
from .views import simple_view

request = HttpRequest()
response = simple_view(request)

然后在response上进行验证。现在我想将上面的内容转换为继承自TemplateView的基于类的视图:

# views.py
from django.views.generic import TemplateView

class SimpleView(TemplateView):
    template_name = 'template.html'

现在基本相同的测试方法失败了:

# tests.py
from django.http import HttpRequest
from .views import SimpleView

request = HttpRequest()
view_func = SimpleView.as_view()
response = view_func(request).render()

结果

Traceback (most recent call last):
    File "tests.py", line 30, in test_home_page_returns_correct_html
response = view_func(request).render()
    File "lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
    File "lib/python2.7/site-packages/django/views/generic/base.py", line 82, in dispatch
if request.method.lower() in self.http_method_names:
AttributeError: 'NoneType' object has no attribute 'lower'

我已尝试将request.method手动设置为GET,但这只会引发另一个错误,抱怨session未进入request

有没有办法通过“空”请求从TemplateView获取响应?

2 个答案:

答案 0 :(得分:5)

感谢dm03513指针!确实,我必须使用RequestFactory,但也要确保request包含空的session(尽管首先列出SessionMiddleware):

# tests.py
from django.test import TestCase
from django.test.client import RequestFactory

class SimpleTest(TestCase):
    def setUp(self):
        self.factory = RequestFactory()

    def test_home_page_returns_correct_html(self):
        request = self.factory.get('/')
        request.session = {}
        view_func = SimpleView.as_view()
        response = view_func(request)
        response.render()
        self.assertIn('<html>', response.content)

答案 1 :(得分:2)

您可以使用内置在测试client中的djangos来实现此目的,而不是直接实例化和调用视图

测试客户端允许您通过django url路由器发出请求

response = self.get('/url/to/invoke/simpleview/')

此外,我发现有几个博客文章涉及如何测试基于类的视图,其中一个是http://tech.novapost.fr/django-unit-test-your-views-en.html