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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -