lundi 7 décembre 2015

Error with Django assertJSONEqual when unit testing view

Similar to this question, I'm having problems testing a view with Django's unit testing framework. My view is very simple: it processes a form, adds an object to the database, and returns a JSONResponse. The test is equally simple, but I keep getting "First argument is not valid JSON: ''. The code actually works in my application; it just doesn't seem to work when unit testing. Any help is appreciated.

My view:

    form = ActionTypeForm(request.POST)
    if form.is_valid():
        action = form.cleaned_data['action']
        new_type = CaseRequestActionType(action=action)
        new_type.save()

        return JsonResponse({'result':'Success', 'msg':'Success'})
    else:
        return JsonResponse({'result':'Fail', 'msg':'An unknown error occurred'})

My test:

def test_post_add_action_type_fails(self):
    response = self.client.post(reverse('zoning:add_action_type'))
    self.assertEqual(response.status_code, 200)
    self.assertJSONEqual(force_text(response.content), {'result':'Fail', 'msg':'An unknown error occurred'})

Aucun commentaire:

Enregistrer un commentaire