angularjs - How to simulate $http timeout in Jasmine and Angular -
i trying simulate http timeout using jasmine. have created angular httpinterceptor
angular.module('coreerrorhandler') .factory('httptimeoutinterceptorservice', function($rootscope, $q, appconfig) { return { 'request': function(config) { config.timeout = 2000; return config; } }; });
and have added service $httpprovider
$httpprovider.interceptors.push('httptimeoutinterceptorservice');
the code works 100%, not sure how test using jasmine.
it('should wait timeout elapse , issue http timeout error', function() { $httpbackend.when("get", "http://google.com").respond({}); var httpgetvalue = $http.get("http://google.com"); settimeout(function(){ $timeout.flush(); console.log(httpgetvalue); expect(something).toequal(false); }, 2100); });
any appreciated. thanks!
there no need use settimeout
here, have pass argument $timeout.flush()
simulate how long time has passed.
it('should wait timeout elapse , issue http timeout error', function() { $httpbackend.when("get", "http://google.com").respond({}); var httpgetvalue = $http.get("http://google.com"); $timeout.flush(2100); // specify how long simulate here console.log(httpgetvalue); expect(something).toequal(false); });
also see: $timeout in ngmock documentation
hope helps.
Comments
Post a Comment