gpt4 book ai didi

javascript - 类型错误 : Cannot set property 'xxx' of undefined

转载 作者:太空宇宙 更新时间:2023-11-04 03:00:13 24 4
gpt4 key购买 nike

我是 typescript 新手,我编写了这段代码来用 Jest 测试方法,

describe('book', () => {

let hostelClient: HostelClient;


it('should book', async () => {

hostelClient.getRequestByIdAsync = jest.fn().mockReturnValue({});

// Assert
let exceptionThrown = false;
expect(exceptionThrown).toBe(false);
});

});

但是我有这个错误:

 TypeError: Cannot set property 'getRequestByIdAsync' of undefined

@Service()
export class HostelClient {

constructor() {
throw new Error('not allowed);
}
..
}

最佳答案

请尝试这个:

describe('book', () => {

let hostelClient: HostelClient = new HostelClient();


it('should book', async () => {

hostelClient.getRequestByIdAsync = jest.fn().mockReturnValue({});

// Assert
let exceptionThrown = false;
expect(exceptionThrown).toBe(false);
});
});

在您的示例中,您定义变量,但没有将该变量实例化为新的 hostelClient。

关于javascript - 类型错误 : Cannot set property 'xxx' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59358380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com