扫一扫分享
Standalone test spies, stubs and mocks for JavaScript. Works with any unit testing framework.
npm install sinon
The following function takes a function as its argument and returns a new function. You can call the resulting function as many times as you want, but the original function will only be called once:
function once(fn) {
var returnValue, called = false;
return function () {
if (!called) {
called = true;
returnValue = fn.apply(this, arguments);
}
return returnValue;
};
}
手机预览