【发布时间】:2017-11-21 21:03:15
【问题描述】:
如何编写一个模拟测试来验证不可访问的属性 (debugLog) 是否设置为 true?我是否试图找到一种方法来找到财产的价值?我是否验证 console.debug 已设置?在这种情况下间谍是否有意义,还是我应该使用存根?
X 级
let showDebugLogs = false,
debugLog = _.noop
/**
* Configures Class X instances to output or not output debug logs.
* @param {Boolean} state The state.
*/
exports.showDebugLogs = function (state) {
showDebugLogs = state;
debugLog = showDebugLogs ? console.debug || console.log : _.noop;
};
单元测试
describe('showDebugLogs(state)', function () {
let spy;
it('should configure RealtimeEvents instances to output or not output debug logs', function () {
spy = sinon.spy(X, 'debugLog');
X.showDebugLogs(true);
assert.strictEqual(spy.calledOnce, true, 'Debug logging was not enabled as expected.');
spy.restore();
});
});
【问题讨论】:
标签: unit-testing mocha.js sinon sinon-chai