我想监视一个简单 TypeScript 类的简单数组。但是,当我设置测试并运行它时,我收到一条错误消息“createDiscounts 不可迭代”,如何模拟可迭代?
这是代码:
// discounts.ts
export function createDiscounts {
discounts: number[] = [0.5, 0.4, 0.7, 0. 9]
return discounts.entries()
}
export class ProcessDiscountedItemsService {
applyDiscounts = (items: OrderItem) => {
for(let [number, discount] of discounts){
//do logic
}
}
}
这是我对 createDiscounts 函数的尝试
import * as DiscountUtil from '../discounts.ts'
describe('Process discount', () => {
const service = new ProcessDiscountedItemsService()
it('process item discounts verify' () =>{
const items = ....
const spy = (jest.spyOn(DiscountUtil, 'createDiscounts')) as jest.Mock
const iteratable = [0.5, 0.4, 0.7, 0. 9].entries()
service.applyDiscounts(items)
})
})
我用mockImplementation替换了mockResolvedValueOnce,这解决了问题