angularjs - Unit test of a function containing promises is not working as expected -


i'm trying unit test controller handles signing user in. i'm getting error , think may have code using promises i'm not sure @ point. here's controller.

angular.module('app').controller('signin', signin);  function signin ($scope, $state, auth) {      $scope.credentials = {};      // signin     $scope.signin = function () {         auth.signin($scope.credentials)             .then(function () {                 $state.go('user.welcome');             }, function (res) {                 $scope.signinerror = res.statustext;             });     }; } 

here's auth services.

angular.module('app').factory('auth', auth);  function auth (session, api) {     return {         signin : function (credentials) {             return api.signin(credentials)                 .then(function (res) {                     session.create(res.data.sessionid, res.data.username);                 });         },         signout: function () {             return session.destroy();         },         isauthenticated: function () {             return !!session.username;         }     }; } 

the api returns $http promises.

signin : function (credentials) {     return $http.post(base + '/auth/credentials', credentials); } 

and session factory store current session info save browser's cookies.

here jasmine test

describe('signin', function () {  // setup ------------------------------------------------------------------      // init app module     beforeeach(module('app'));      // inject session service     beforeeach(angular.mock.inject(function (_$rootscope_, _$controller_, _auth_) {         $rootscope             = _$rootscope_;         $scope                 = $rootscope.$new();         controller             = _$controller_("signin", {$scope: $scope});         auth                   = _auth_;     }));      // setup spies     beforeeach(function () {         spyon(auth, 'signin');     });  // tests ------------------------------------------------------------------      // signin in     it("should call auth.signin user's credentials", function () {         $scope.signin();         expect(auth.signin).tohavebeencalled();     });  }); 

i'm trying test auth.signin called when call $scope.signin(). application working getting error when trying run test.

signin should call auth.signin user's credentials failed typeerror: 'undefined' not object (evaluating 'auth.signin($scope.credentials).then') 

although there expectation there, error happening when call $scope.signin(); if log auth right before $scope.signin(); called this.

log: object{signin: function () { ... }, signout: function () { ... }, isauthenticated: function () { ... }} 

so assume di working expected. i've looked @ different ways of setting spies haven't figured out wrong yet. appreciated.

that because spying on auth's signin function. when spy on it, wont real function in api more. need send promise spy using .and.returnvalue.

so do:-

beforeeach(inject(function (_$rootscope_, _$controller_, _auth_, _$q_) {     $rootscope             = _$rootscope_;     $scope                 = $rootscope.$new();     controller             = _$controller_("signin", {$scope: $scope});     auth                   = _auth_;     fakepromise = _$q_.when(); //get fake promise }));  beforeeach(function () {   spyon(auth, 'signin').and.returnvalue(fakepromise); //return fakepromise }); 

plnkr

and if return value fro signin (or other function's promise, do)

  fakeauthpromise = _$q_.when(somedata);  

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -