javascript - How to test a service that is called on .success of http.post -


i have controller calls service performs http.post username , password. on .success of post, call service create cookie.

i 'believe' have created successful jasmine test test post, unsure of how test cookie service.

logincontroller.js:

app.controller('logincontroller', function($scope, $http, signinservice, cookiesrv) {      $scope.login = function(usrnm, pwd) {         signinservice.authuser(usrnm, pwd)             .success(function (data, status, headers, config) {                 var cookieid = 'mycookie';                 cookiesrv.createcookie(cookieid, data.token, 3, data.redirecturl);             })             .error(function (data, status, headers, config) {                // display error message             }     } }); 

cookiesrv.js

app.service('cookiesrv', function() {     return {         createcookie : function(cookieid, token, days, redirecturl) {             if (days) {                 var date = new date();                 date.settime(date.gettime()+(days*24*60*60*1000));                 var expires = "; expires="+date.togmtstring();             }             else var expires = "";             document.cookie = cookieid+"="+token+expires+"; path=/";              window.location.assign(redirecturl)         }     } }); 

signinservice:

app.service('signinservice', function($http) {         this.authuser = function (usrnm, pwd) {             return $http.post('/api/json/authenticate', {}, {             headers: {                 'content-type': 'application/json',                 'x-app-username': usrnm,                 'x-app-password': pwd             }         });     }; }); 

unit test:

describe('controller: logincontroller', function() {      beforeeach(module('myapp'));      beforeeach(inject(function($controller, $rootscope, signinservice, $httpbackend){         this.$httpbackend = $httpbackend;         this.scope = $rootscope.$new();         $controller('logincontroller', {             $scope: this.scope,             localsigninservice: signinservice         });     }));          describe("successfully logged in", function() {         it("should redirect home", function() {              var fakeresponse = {                 access_token: 'mytoken'             }              this.$httpbackend.expectpost('/api/json/authenticate', {} , function(headers) {                 return {                     'content-type': 'application/json',                     'x-app-username': 'tomcat',                     'x-app-password': 'tomcat'                 };             }).respond(200, fakeresponse);              this.scope.login("tomcat","tomcat");             this.$httpbackend.flush();             expect(fakeresponse.access_token).toequal("mytoken");         });     }); }); 

update:

with above getting error:

error of tests did full page reload!  

some research suggests because of window.location.assign in cookie service. if comment out line, test runs fine without error above.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -