reactjs - TypeError:fetch.mockResponse 不是函数
问题描述
遇到以下错误:TypeError: fetch.mockResponse is not a function
尝试测试以下内容:
getRequests = () => {
let headersObj = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': this.props.services.Dashboard.subscription_key,
}
fetch(`${this.props.services.Dashboard.URL}/REQUEST`, {
headers: headersObj,
method: 'GET',
credentials: 'include',
}).then(res => res.json())
.then(json => {
if(json.value) {
this.setState({Requests: json.value, allowNext: true})
}
})
}
到目前为止,这是我所拥有的:不确定我是否走在正确测试的正确道路上,但我将不胜感激,谢谢
beforeEach(() => wrapper = mount(<MemoryRouter keyLength={0}><FileName {...baseProps} /></MemoryRouter>)
it('Test getRequests function ',() => {
wrapper.find('FileName').setState({
Requests: [],
allowNext: true
});
fetch.mockResponse(JSON.stringify([]));
wrapper.update();
expect(wrapper.find('FileName').instance().getRequests('test')).toEqual()
expect(wrapper.find('FileName').state('allowNext')).toBeTruthy();
解决方案
mockResponse
不是 Fetch API 中的内置函数 - 看起来您已经遵循了一个使用jest-mock-fetch库的示例,该库添加了各种模拟辅助函数,包括mockResponse。
推荐阅读
- javafx - 有没有办法在javafx中制作图像饱和动画?
- node.js - 使用 pdf 填充程序时遇到 AWS Lambda 问题
- python - Keras 1.2.2 model inside other model
- c# - 如何修复组件启动时的控制台错误
- node.js - 如何更改谷歌 lightouse 报告的语言
- ansible - 分子:如何查看有关受管理机器的“事实”,例如“ansible -m setup instance1”
- kubernetes - 反模式 - 在 Pod 启动期间构建(Init-Container)?
- android - CMakeLists.txt "-Wl,--whole-archive" 选项在三星 Galaxy Note2、S3 (Kitkat 4.4.2) 上导致 SIGABRT 错误
- c++ - 在 C++ 中没有调用“game_rule”的匹配函数
- oracle - Google Cloud HSM 能否为本地 Oracle 数据库 TDE 托管 KEK?