我試圖使用這個NodeJS Kubernetes客戶端。 https://github.com/godaddy/kubernetes-client#examples
在單元測驗中使用該客戶端模擬我的呼叫時,我有些糾結。鑒于上面鏈接的例子,在最后一行模擬get()呼叫的最佳方式是什么?
// Using a little wrapper to return my custom instantiated kubernetes-client
const getClient = require('./utils/getClient'/span>)
//讓我們假設我們是在一個異步函式中。
const myClient = await getClient()
const deploymentConfigs = await myClient.apis.apps.v1。 namespaces('mynamespace').deployments.get()
我在測驗中嘗試了這種方法,但沒有成功 :
const getClient = require(' ./utils/getClient' )
jest.mock('./utils.getClient')
const mockGet = jest.fn()
getClient.mockImplementation(() => {
return {
apis: jest.fn().mockReturnThis() 。
apps: jest.fn().mockReturnThis() 。
v1: jest.fn().mockReturnThis() 。
namespaces: jest.fn().mockReturnThis() 。
buildconfigs: jest.fn().mockReturnThis() 。
get: mockGet
}
})
使用這個getClient.mockImplementation我一直收到以下錯誤:
TypeError。不能讀取屬性of undefined(讀取'v1')
當這一行被呼叫時:
const deploymentConfigs = await myClient.apis.apps. v1.namespaces('mynamespace'/span>).deployments.get()
uj5u.com熱心網友回復:
你可以嘗試這樣模擬:
你可以嘗試這樣模擬。
const namespaces = jest.fn().mockReturnValue( {
deployments: {
get: jest.fn().mockResolvedValue(mockDeployments)。
},
});
getClient.mockResolvedValue( {
apis: {
apps: {
v1: {
命名空間。
},
},
},
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/310294.html
標籤:
