我有一个设置 token 到期日期的方法:
var jwt = require('jwt-simple');
module.exports = {
setExpirationDate: function(numDays) {
var dateObj = new Date();
console.log(dateObj);
}
}
我想在“新日期”语句上写一个断言:
var jwtHelper = require('../../../helpers/jwtToken');
describe('setExpirationDate method', function() {
it('should create date object', function() {
var Date = sinon.spy(Date);
jwtHelper.setExpirationDate(global.TOKEN_EXPIRE_DAYS);
expect(Date).to.be.called;
});
});
测试失败:
AssertionError: expected spy to have been called at least once, but it was never called
构造函数 spy 有什么需要注意的地方吗?
最佳答案
考虑到您的构造函数绑定(bind)到“全局”,这意味着如果您在浏览器上打开开发者控制台,您应该能够通过使用相关的函数/构造函数来实例化一个对象:
var Date = new Date();
如果是这样,实际的工作代码可能是:
var Date = sinon.spy(global, 'Date');
expect(Date.called).to.be.equal(true);
关于javascript - 用 sinon 监视 Date 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32338427/