如何使用 SinonJS 判断 属性 是否存在?

How to determine if property exists using SinonJS?

我一直在搜索,但找不到一个简单的 sinon 示例(断言?)来检查函数 .doSomething 是否存在。

var sinon = require('sinon');
var myModule = require('my-module');

describe('myModule', function () {
  it('has a function .doSomething', function () {
    // check whether typeof myModule.doSomething === 'function'
  });
});

检查此函数是否存在的正确方法是什么?

这应该使用断言库来完成,而不是 sinon(这是一个 stub/mock 库)。

Chai 是与 mocha 一起使用的流行选项:

var expect = require('chai').expect;
var myModule = require('my-module');

describe('myModule', function () {
  it('has a function .doSomething', function () {
    expect(myModule.doSomething).to.be.a('function')
  });
});
sinon.assert.match(myModule.doSomething, sinon.match.func);

sinon.assert.match(typeof myModule.doSomething, "function");