dimanche 2 décembre 2018

Django testing - test the view which handles an ajax call

In testing Django, the view is returning 200 code but not sending any error message related.

def ajax_view(request):
    msg = ''
    if request.is_ajax():
        username = request.POST['username']
        user = User.objects.get(username=username)
        msg = 'user exists'

    return HttpResponse(msg)

In tests.py

    response = self.client.post(reverse('ajax_view'), data={'username': 'hello'})       
    self.assertEqual(200, response.status_code)
    self.assertContains(response, 'exist')

It seems it is not going through the request.is_ajax().. How can I mock the ajax call in Django testing?

Aucun commentaire:

Enregistrer un commentaire