javascript - Mocha/Chai 检查对象原型上是否存在键
问题描述
我正在使用 Mocha/Chai 编写测试,并且assert.hasAllKeys
适用于不在对象原型上的键。有没有办法检查对象原型上是否存在键?
我试过阅读文档无济于事。
谢谢
解决方案
我们可以使用这种方式
assert.hasAllKeys(YourObject.prototype, ['prop1', 'prop2']);
这是代码示例:
src.js
function MyObject () {
}
MyObject.prototype.prop1 = function() {
console.log('ok');
}
MyObject.prototype.prop2 = 10;
module.exports = {
MyObject
};
测试.js
const chai = require('chai');
const src = require('./src');
const assert = chai.assert;
describe('unit test', function() {
it('runs test', function() {
assert.hasAllKeys(src.MyObject.prototype, ['prop1', 'prop2']); // check object prototype
})
});
希望能帮助到你
推荐阅读
- flutter - 谷歌表格颤动
- javascript - 如何获得旧消息反应
- azure - Azure 数据流 - 我们可以为 Unpiovt 功能设置动态列或更改预测吗
- image - flask return pil 生成图像错误,我可以看到调整大小的图片但响应图像是错误的
- python - 将字典列表写入 S3 文件中的单独行
- python - 使用python时neo4j内存不足错误
- node.js - 如何构建高度安全的端到端加密 React Native 消息传递应用程序
- typescript - 如何验证该文件是否已在 testcafe 中下载?
- node.js - 在 React 应用程序中的多个输入文件上运行 ffmpeg (WASM/NodeJS)
- node.js - 无法将 npm 从 6.14.8 更新到 7.3;删除 npm.cmd、npx.cmd 和 npm 会导致更多错误