2017-08-17 59 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')'。 –