javascript - “获取”未定义
问题描述
我有这段代码,我正在用 HTMLUnit 测试:
function fetchIt() {
var response = fetch('https://httpbin.org/get');
var json = response.json();
console.log(json);
return json;
};
fetchIt();
但是抛出
net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "fetch" is not defined.
我们如何使用fetch
HTMLUnit、JUnit 进行测试?
fetch
如https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
解决方案
使用HtmlUnit测试代码意味着您不在具有fetch 功能的浏览器中。
您应该在页面中包含用于 fetch 的 polyfill,或使其在单元测试上下文中可用。
推荐阅读
- html - 将图像放入使用 css 伪元素处理的方形 div
- c# - 如何正确将 16Bit 字节数组转换为音频剪辑数据?
- javascript - 在字符串中显示 [object Promise]
- reactjs - 排毒 e2e 测试分配给未定义并跳过所有测试
- java - 有什么方法可以阻止我的 Android 应用在后台扫描 nfc 标签?
- azure-functions - Azure 函数输出绑定到 Cosmos DB 错误与 PartitionKey
- azure-artifacts - Azure DevOps 自托管代理无法从 Azure 工件下载工件
- javascript - JSDoc:如何将变量描述为类构造函数?
- discord.js - 防止XP系统中的垃圾邮件?
- episerver - 如何更改 Episerver Forms 表单元素的 ID 属性?