Skip to content

Instantly share code, notes, and snippets.

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

  • Save lele85/4c57a475acf3b1008870 to your computer and use it in GitHub Desktop.

Select an option

Save lele85/4c57a475acf3b1008870 to your computer and use it in GitHub Desktop.
Add timeout to a call in test angularjs
var backend_mock_timeout = function (par) {
angular.module('httpBackendMock', ['ngMockE2E'])
.run(function ($httpBackend) {
$httpBackend.whenGET(/.*/).passThrough();
$httpBackend.whenPOST(/check-barcode/).respond(function(){
return [0,{}];
});
$httpBackend.whenPOST(/.*/).passThrough();
});
.config(["$provide",function($provide) {
$provide.decorator('$httpBackend', ["$delegate", "$timeout", function($delegate, $timeout) {
var proxy = function(method, url, data, callback, headers) {
var timeout = url.match(/check-barcode/) ? 10000 : 0;
var interceptor = function() {
var _this = this,
_arguments = arguments;
$timeout(function() {
callback.apply(_this, _arguments);
}, timeout);
};
return $delegate.call(this, method, url, data, interceptor, headers);
};
for(var key in $delegate) {
proxy[key] = $delegate[key];
}
return proxy;
}]);
}]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment