Mocha/Chai 检查对象原型上是否存在键

Mocha/Chai check if keys exist on an objects prototype

我正在使用 Mocha/Chai 编写测试,assert.hasAllKeys 适用于不在对象原型上的键。有没有办法检查对象原型上是否存在键?

我已经尝试阅读文档但无济于事。

谢谢

也许:

/**
*  @returns bool true if propName exists in proto only
*/
const propInProtoOnly = (propName, objToCheck) =>
propName in objectToCheck && !objectToCheck.hasOwnProperty(propName)

够好吗? )

我们可以这样使用

assert.hasAllKeys(YourObject.prototype, ['prop1', 'prop2']);

这是代码示例:

src.js

function MyObject () {

}

MyObject.prototype.prop1 = function() {
  console.log('ok');
}

MyObject.prototype.prop2 = 10;

module.exports = {
  MyObject
};

test.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       
  })
});

希望对您有所帮助