AngularJS - Unit test with header response -


my backend retrieves me 'x-token' when user log in on web. on frontend simple validation, getting token , setting: $rootscope.authenticated = true; if token not present on header response reason frontend sets: $rootscope.authenticated = false;

my controller

signinservice     .signin(datafromform)     .then(function (data) {         if ( $mytokenmanager.save(data.headers('x-token')) ) {             $rootscope.authenticated = true;         } else {             $rootscope.authenticated = false;             // unit test sent here :(         }      }); 

this working perfect, need unit test method, test not generating or setting header response properly. doing wrong on test?

describe('testing controllers', function() {      describe('mycontroller unit test', function() {          var $httpbackend, $rootscope, createcontroller, authrequesthandler, datafromform;          beforeeach(module('myapp'));          beforeeach(inject(function($injector) {              $httpbackend = $injector.get('$httpbackend');              authrequesthandler = $httpbackend                                     .when(                                         'post',                                         'http://mywebapp:8080/loginuser',                                         {"username":"admin", "password":"admin"}                                     ).respond({'x-token': 'xxx'});              $rootscope = $injector.get('$rootscope');             var $controller = $injector.get('$controller');              createcontroller = function() {                 return $controller('mycontroller', {'$scope' : $rootscope});             }         }));          //         aftereach(function() {             $httpbackend.verifynooutstandingexpectation();             $httpbackend.verifynooutstandingrequest();         });          //         it('should authentication user', function() {              var controller = createcontroller();              //             $rootscope.username = "admin";             $rootscope.password = "admin";              datafromform = {                 username: $rootscope.username,                 password: $rootscope.password             };              $httpbackend.expectpost(                 'http://mywebapp:8080/loginuser',                 {"username":"admin", "password":"admin"}             ).respond(                 200,                  {'x-token': 'xxx'}             );              $rootscope.login(formdata);             $httpbackend.flush();             expect($rootscope.authenticated).tobe(true); // getting false :(         });          });  }); 

i'm following this example on angular documentation.

the documented signature of respond() function is

function([status,] data[, headers, statustext]) 

in first respond(), pass data, , no header:

respond({'x-token': 'xxx'});     

in second respond(), pass status , data, , still no header:

respond(     200,      {'x-token': 'xxx'} ); 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -