2016-09-23 58 views
0

{{productId1}}等於1郵差:全局變量存儲爲字符串?

這是請求:

localhost:9000/test/product/{{productId1}} 

它的工作原理,並返回我:

{ 
    "productId": 1, 
    "title": "Hat X", 
    "description": "This is Hat X." 
} 

但是,它不是測試:

var jsonData = JSON.parse(responseBody); 
tests["Status code is 200"] = responseCode.code === 200; 
tests["Product ID"] = jsonData.productId === "{{productId1}}"; 
tests["Title"] = jsonData.title === "Hat X"; 
tests["Description"] = jsonData.description === "This is Hat X."; 

第三行保持失敗。到底是怎麼回事?是否因爲productId1被讀爲字符串?如果是這樣,我試圖解析它爲整數,沒有運氣。我怎樣才能通過它而不失去這種類型?

我想:

tests["Product ID"] = jsonData.productId === "{{productId1}}"; 
tests["Product ID"] = jsonData.productId === "{{$productId1}}"; 
tests["Product ID"] = jsonData.productId === {{productId1}}; 
tests["Product ID"] = jsonData.productId === productId1; 

回答

2

爲了獲得在測試窗口中的全局變量,你應該使用 'postman.getGlobalVariable()' 功能。

在你的情況,它應該是這樣的:

測試[ 「產品ID」] = jsonData.productId === postman.getGlobalVariable( 「productId1」);

+0

郵差根本沒有這樣的功能! –

+1

絕對有:看看這裏。 https://docs.omniref.com/js/npm/newman/1.1.4/symbols/postman.getGlobalVariable在發佈之前,我已經測試了上面的代碼。 –

+0

你是對的,對不起。請更新您的答案,以便我可以放棄它,否則它不會讓我這樣做。 –