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

Тел: +7 965 3737 888





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

Ответы (5):

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

@cooncesean: this looks like it works ->


request.csrf_processing_done = True


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

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

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

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

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

Answering myself, replace the last line with the following:

        request = WSGIRequest(environ)
        handler = BaseHandler()
        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

Ответ полезен? Да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.