javascript - angular resource promise catch, jasmine toThrow() -


i have code in angular controller:

user angular $resource returns promise when method called.

$scope.credentials = {         username:"",         password:"",         rememberme:false     };  var currentuser  = {};  $scope.login = function(callback){         user.get($scope.credentials)             .$promise             .then(function(user){                 $scope.currentuser = user;                 return cb ? cb(user) : user;             })             .catch(function(res){                 throw "loginerror";             });  }; 

i'm trying test whether throws error or not jasmine so:

expect(function(){   scope.login(); }).tothrow(); 

but error thrown:

expected function throw exception.

i have tested acceptance of promise, works expected, i'm assuming theres asynchronous aspect i'm not able deal with.

i tried calling passing in , calling done() didn't work either.

edit:

i mocking out backend so:

beforeeach(inject(function($rootscope,$controller,_$httpbackend_){                $httpbackend = _$httpbackend_;             successcallback = jasmine.createspy();             errorcallback = jasmine.createspy();               scope = $rootscope.$new();             controller = $controller('logincontroller',{                 '$scope':scope             });          }));  aftereach(function(){             $httpbackend.verifynooutstandingexpectation();             $httpbackend.verifynooutstandingrequest();         }); 

note other tests work, have tried other scenarios server returns 200 , user , works. testing whether $scope.login() throws error when receives server error.

you need setup $httpbackend mock response of resource. need flush these results have control on when resolve.

i put quick example of scenario on plunker.

http://plnkr.co/edit/lnwas1lyjja56zh1styl?p=preview

the relevant spec file. http://embed.plnkr.co/lnwas1lyjja56zh1styl/appspec.js

snippet @ glance:

$httpbackend.expectget('/api/index.php/user')                 .respond(500, 'error'); //initiate request calling api/resource $httpbackend.flush(); 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -