javascript - Karma - Spied function error handling -


i have service this:

app.service('usersservice.v2', ['$http', '$q', '$exceptionhandler', 'user', 'uservalidator.v2', 'crud', function($http, $q, $exceptionhandler, user, uservalidator, crud){     function dummypromise(){         var dummydeferred = $q.defer();         dummydeferred.resolve('dummy');          return deferred.promise;     }      this.getuser = function(userid, companyid){         try{             uservalidator.validateid(userid);             uservalidator.validateid(companyid);         }         catch(e){             $exceptionhandler(e);             return dummypromise();         }          return $http.get(apiurl + 'api/v2/companies/' + companyid + '/users/' + userid)             .then(function(response){                 var user = new user(response.data);                  try{                     uservalidator.validateuser(crud.read, user);                 }                 catch(e){                     $exceptionhandler(e);                     return;                 }                  return user;             })     }; }]); 

and want test behaviour of service depending on validation functions do. uservalidator.* function if/else blocks throwing errors.

in karma have this:

describe('service: usersservice.v2', function () {     var usersservice, httpbackend, state, uservalidator;     const  url = 'address'      function _inject() {         angular.mock.inject(function ($injector) {             usersservice = $injector.get('usersservice.v2');             uservalidator = $injector.get('uservalidator.v2');             httpbackend = $injector.get('$httpbackend');         });     }      beforeeach(function () {         angular.mock.module('app');         _inject();     });      describe('getuser', function () {         beforeeach(function () {             httpbackend.when('get', url);         });         aftereach(function () {             httpbackend.verifynooutstandingexpectation();             httpbackend.verifynooutstandingrequest();         });      it('should return dummy promise if id validation fails', function(){             spyon(uservalidator, 'validateid').and.throwerror('missing or wrong id thrown');             usersservice.getuser()                  .then(function(data){expect(data).tobe('dummy');})         });     )}; }) 

but when i'm running karma error, if didn't put catch handle expection , following block of code not executed.

what doing wrong?

cheers, manuel


update: validate methods this:

... code code code ... this.validateid = function(id){     if(!id || !angular.isnumber(id)) throw 'missing or wrong id'; } 

thus problem karma trying handle error thrown validation instead of let userservice it.

you testing usersservice.v2.you can not test uservalidator.v2 @ same time.but can mock uservalidator.v2 service.

var uservalidator; beforeeach(function() {      module(function($provider) {         uservalidator = {             validateid: function(id) {                  if (id === 123 ||id === 456 ) { //put mock test id here pass case                     return true                 }                 return false;             }         }         $provider.value('uservalidator.v2', uservalidator);     }) }); describe('getuser', function () {         beforeeach(function () {             httpbackend.when('get', url)             .respond(200, {                     data: "dummy"                 });;         });       it('should return dummy promise if id validation fails', function() {          usersservice.getuser(9879,8798) //its failed case             .then(function(data) { expect(data).tobe('dummy'); })      })  }) 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -