如何使用SinonJS确定属性是否存在?

时间:2018-04-12 18:15:02

标签: javascript sinon

我一直在搜索,找不到一个简单的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'
  });
});

检查此功能是否存在的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

sinon.assert.match(myModule.doSomething, sinon.match.func);

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

答案 1 :(得分:0)

这应该使用断言库,而不是sinon(这是一个存根/模拟库)。

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')
  });
});