相关疑难解决方法(0)

使用django测试客户端发送JSON

我正在开发一个django项目,它将作为webhook的端点.webhook会将一些JSON数据发送到我的端点,然后端点将解析该数据.我正在尝试为它编写单元测试,但我不确定我是否正确地发送了JSON.

我一直在pipeline_endpoint中得到"TypeError:string indices必须是整数"

这是代码:

# tests.py
from django.test import TestCase
from django.test.client import Client
import simplejson

class TestPipeline(TestCase):

    def setUp(self):
        """initialize the Django test client"""
        self.c = Client()

    def test_200(self):
        json_string = u'{"1": {"guid": "8a40135230f21bdb0130f21c255c0007", "portalId": 999, "email": "fake@email"}}'
        json_data = simplejson.loads(json_string)
        self.response = self.c.post('/pipeline-endpoint', json_data, content_type="application/json")
        self.assertEqual(self.response.status_code, "200")
Run Code Online (Sandbox Code Playgroud)

# views.py
from pipeline.prospect import Prospect
import simplejson

def pipeline_endpoint(request):

    #get the data from the json object that came in
    prospects_json = simplejson.loads(request.raw_post_data)
    for p in prospects_json:
        prospect = { …
Run Code Online (Sandbox Code Playgroud)

django json unit-testing

36
推荐指数
6
解决办法
3万
查看次数

标签 统计

django ×1

json ×1

unit-testing ×1