vero4karu
5/20/2014 - 4:01 PM

Django: ajax json view in tests

Django: ajax json view in tests

import json
from django.test import TestCase

class JSONViewTestCase(TestCase):

    def test_json_view(self):
        response = self.client.post(
            reverse('my_json_view'),
            json.dumps({
                # your JSON
            }),
            'json',
            HTTP_X_REQUESTED_WITH='XMLHttpRequest',
        )
        json_string = response.content
        response_data = json.loads(json_string)
        # do smth.