admin管理员组文章数量:1356253
Problem
In our codebase we have a problem with sinon which can be reproduced with the code snipped below. The thing is that it seems to be that indirect called spies return force false
, the console.log
clearly states that the method is called but the spy.called
remains false
.
Code
The following CDN's can be used for the html:
//cdnjs.cloudflare/ajax/libs/sinon.js/1.7.3/sinon-min.js
//cdnjs.cloudflare/ajax/libs/require.js/2.1.14/require.min.js
main.js
require(['myModule'], function(module) {
//using sinon
var methodCallerSpy = sinon.spy(module, 'methodCaller')
console.log(methodCallerSpy); // methodCaller
module.methodCaller();
console.log(methodCallerSpy.called); //true
var methodSpy = sinon.spy(module, 'method');
console.log(methodSpy); //method
module.methodCaller();
console.log(methodSpy.called); // false
module.method();
console.log(methodSpy.called); // true
});
And the module
define(function() {
const method = () => console.log('method called by methodCaller');
const methodCaller = () => method();
return{
method,
methodCaller
}
});
Problem
In our codebase we have a problem with sinon which can be reproduced with the code snipped below. The thing is that it seems to be that indirect called spies return force false
, the console.log
clearly states that the method is called but the spy.called
remains false
.
Code
The following CDN's can be used for the html:
//cdnjs.cloudflare./ajax/libs/sinon.js/1.7.3/sinon-min.js
//cdnjs.cloudflare./ajax/libs/require.js/2.1.14/require.min.js
main.js
require(['myModule'], function(module) {
//using sinon
var methodCallerSpy = sinon.spy(module, 'methodCaller')
console.log(methodCallerSpy); // methodCaller
module.methodCaller();
console.log(methodCallerSpy.called); //true
var methodSpy = sinon.spy(module, 'method');
console.log(methodSpy); //method
module.methodCaller();
console.log(methodSpy.called); // false
module.method();
console.log(methodSpy.called); // true
});
And the module
define(function() {
const method = () => console.log('method called by methodCaller');
const methodCaller = () => method();
return{
method,
methodCaller
}
});
Share
Improve this question
edited May 12, 2017 at 14:12
Flame_Phoenix
17.6k40 gold badges144 silver badges284 bronze badges
asked Sep 25, 2014 at 14:25
jeroenoliemansjeroenoliemans
2364 silver badges9 bronze badges
1 Answer
Reset to default 8The problem is that myModule
has two private functions called method()
and methodCaller()
as well as two methods it exposes with the same names.
Sinon is capable on spying on the exposed methods, but not the internal functions.
When you call module.method()
your invoking the exposed method, so Sinon is able to detect the call. However, when you call method.methodCaller()
, methodCaller()
calls the method()
private function directly, and therefore the call is not detected by Sinon.
If you change your methodCaller()
function to:
methodCaller = function(){
this.method();
}
... then Sinon should be able to capture the "indirect" call to method()
when calling methodCaller()
.
本文标签: javascriptSinon Spy is not called if the spied method is called indirectlyStack Overflow
版权声明:本文标题:javascript - Sinon Spy is not called if the spied method is called indirectly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744027924a2578376.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论