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
Post a Comment