[英]Expect object to have all given properties
我正在使用Jest為我的Node.js應用程序編寫單元測試,並且我正在嘗試測試API端點是否返回具有一組預定屬性的對象。 我有以下內容:
const sampleResponse = {
"Game": "GameName",
"World": "2",
"Location": "ServerLocation",
"Timestamp": 1516204557853,
"alive": true,
"time": 10.1,
"min": 8.927,
"max": 10.154,
"avg": 9.409
};
然后我使用以下代碼來測試對象屬性:
test("Location for server cluster exists, and returns an array of objects", () => {
return Request(Routes.LocationSpecific).then((result) => {
expect(result[0]).toMatchObject(sampleResponse);
});
});
其中result
是包含與上述對象類似的數據的對象數組,我只是匹配result[0]
。
測試失敗,因為從我的API端點返回的每個對象屬性的值與上面的示例數據不匹配。 屬性存在,紅色出現在我的終端中,不匹配的值,但我想讓它變綠。
有沒有辦法將result[0]
所有給定屬性與樣本對象相匹配,無論它們的值如何?
比較鍵的數組:
expect(Object.keys(result[0])).toEqual(Object.keys(sampleResponse));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.