2017-08-17 47 views
0

我有一个金字塔网络应用程序,我试图进行单元测试。金字塔单元测试发送参数

在我的测试文件,我有这样的代码片段:

anyparam = {"isApple": "True"} 
@parameterized.expand([ 
    ("ParamA", anyparam, 'success')]) 
def test_(self, name, params, expected): 
    request = testing.DummyRequest(params=params) 
    request.session['AI'] = '' 
    response = dothejob(request) 

    self.assertEqual(response['status'], expected, "expected response['status']={0} but response={1}".format(expected, response)) 

而在我的观点:

@view_config(route_name='function', renderer='json') 
def dothejob(request): 
    params = json.loads(request.body) 
    value = params.get('isApple') #true or false. 

然而,当我试图单元测试,我越来越这个错误:

JSONDecodeError: Expecting value: line 1 column 1 (char 0) 

但是,当我通过网络浏览器通过POST发出相同的请求时,它工作得很好。

回答

0

通过做testing.DummyRequest(params=params)你只填充request.params,而不是request.body

你可能想这样做:

request = testing.DummyRequest(json_body=params) 

此外,你可能想直接request.json_body在你的代码,而不是json.loads(request.body)使用。

+0

如果我这样做有一个属性错误:NoneType'对象没有属性。当我尝试调试时,无作为参数发送。我也试过 - request.json_body和json.loads(request.body),甚至把它们放在try/except中。 – theconqueror972

+0

好吧,我真的没有时间去调试这个,但是你可以尝试执行'testing.DummyRequest(body = json.dumps(params).encode('utf-8'))',或'request = testing .DummyRequest()'then'request.body = json.dumps(params).encode('utf-8')'。 –