Задать вопрос

Тел: +7 965 3737 888

539

Просмотров

5

Ответов

RequestFactory: Easily create mock request objects, for use in testing

Django's testing framework assumes you will be running your tests against "live" views that have been plugged in to your site's URL configuration - but sometimes you might want to run a test against a view function without first wiring it in to the rest of the site. This class makes it easy to do that by providing a "factory" for creating mock request objects, re-using the existing test Client interface (and most of the code). Once you've created a request object in your test you can use it to call your view functions directly, then run assertions against the response object that gets returned.

Вопрос полезен? Да0/Нет0
file_3118.py(1.2Кб)
None

Ответы (5):

Ответcooncesean:24.02.2011
Ответ полезен? Да0/Нет0

@cooncesean: this looks like it works ->

....

request.csrf_processing_done = True

....

Ответcooncesean:24.02.2011
Ответ полезен? Да0/Нет0

Wondering if there's any solution for sending this request to a view that has the @csrf_protect_m decorator.

Ответpeterbe:29.09.2009
Ответ полезен? Да0/Нет0

Is this still sufficient? You cut away large chunks of Client.request I notice by checking the SVN version.

Ответakaihola:16.02.2009
Ответ полезен? Да0/Нет0

Answering myself, replace the last line with the following:

        request = WSGIRequest(environ)
        handler = BaseHandler()
        handler.load_middleware()
        for middleware_method in handler._request_middleware:
            if middleware_method(request):
                raise Exception("Couldn't create request mock object - "
                                "request middleware returned a response")
        return request

Ответakaihola:16.02.2009
Ответ полезен? Да0/Нет0

What would be a good way to have request.user included? Just assign a User object to the created request? It would be nice to have AnonymousUser there by default.