使用SinonJS 3运行测试我面临以下问题
测试有什么问题?
console.log(spyRequester.callCount);
第var message = 'Welcome to the AWS Podcast. You can say, play the audio to begin the podcast.';
var reprompt = 'You can say, play the audio, to begin.';
行
应该打印1但它打印0
答案 0 :(得分:1)
您当前的createIfNotExists
实施正在调用其范围内捕获的_doCreate
。如果你想让它运行你的模拟,你应该以某种方式使这些函数之间的“链接”动态。在您的特定情况下,使用this._doCreate
应该有效。但如果使用不正确的上下文调用,它会使createIfNotExists
失败。
var creator = (function() {
var createIfNotExists = function createIfNotExists() {
// pick _doCreate from the context or use default.
(this._doCreate || _doCreate)();
};
var _doCreate = function _doCreate() {
console.log('_doCreate was called');
};
return {
createIfNotExists:createIfNotExists,
_doCreate:_doCreate
};
}());
var util = {
createIfNotExists:creator.createIfNotExists,
_doCreate:creator._doCreate
};
var spyRequester = sinon.spy(util, '_doCreate');
util.createIfNotExists();
console.log(spyRequester.callCount); // prints 1
<script src="https://unpkg.com/sinon@3.2.1/pkg/sinon-3.2.1.js"></script>