angularjs - Angular and Jasmine tests with mock $httpBackend -


i'm trying test method uses $http service mocking $httpbackend. method works fine in app, can't test work. here test code not work (i not result, , url correct:

describe("authhelper", function() {     var webhelper;     var url;     var $httpbackend;      beforeeach(function(){         pingresponse = {             principal: null,             authenticated: false,             servlet:true,             timestamp:1468325333070         };         angular.mock.module('provisioning');          inject(function ($injector) {             webhelper = $injector.get('webhelper');         url = webhelper.buildurl('/ping');             $httpbackend = $injector.get('$httpbackend');             $httpbackend.whenget(webhelper.buildurl('/ping')).respond(200, pingresponse);         });     });      it("should pass if setup correct", inject(function(authhelper) {         expect(true).tobe(true);     console.log(url);         authhelper.isauthenticated().then(         function(data){ alert(data); },         function(data){ alert(data); }     );     })); 

here method testing - works when have app service running on server, mock not working. have 10 other tests work fine, can't seem mock working. see url logged in console that's it:

var isauthenticated = function(){     var d = $q.defer();     $http.get(webhelper.buildurl('/ping')).then(function(data){     console.log("isauth got");     console.log(data);          if(data.data.authenticated){             d.resolve(true);         } else {             d.resolve(false);         }     }, function(errordata){     console.log("error");     console.log(errordata);     d.resolve(false); });      return d.promise; }; 

you'll need call httpbackend.flush(); flush pending requests after calling authhelper.isauthenticated().

see angular documentation more details: https://docs.angularjs.org/api/ngmock/service/$httpbackend


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -