Skip to content

Instantly share code, notes, and snippets.

@lancecaraccioli
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save lancecaraccioli/fa6b0977f27d7b1ff9ab to your computer and use it in GitHub Desktop.

Select an option

Save lancecaraccioli/fa6b0977f27d7b1ff9ab to your computer and use it in GitHub Desktop.
Jasmine spy + AngularJS $timeout = lodash.debounce mock for AngularJS unit testing
/**
* @description
* Rudimentary mock of lodash.debounce, which assumes the default debounce options
* - Jasmine 2.0.0
* - lodash 2.4.1
* - AngularJS 1.3.0
*/
function mockLodashDebounceForAngularJS(fn, ms) {
var callPromise;
return function () {
$timeout.cancel(callPromise);
//console.info('debounce "' + fn.name + '" ' + ms + 'ms');
callPromise = $timeout(fn, ms);
/*callPromise.then(function () {
console.info(fn.name + ' was called after debounce of ' + ms + 'ms');
});*/
};
}
//via jasmine spyOn
spyOn(_, 'debounce').and.callFake(mockLodashDebounceForAngularJS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment