2015-05-14 68 views
0

我想测试我的控制器中正在调用我的注入服务。注入服务的单元测试控制器

login.controller.js

angular.module('exampleModule') 
    .controller('LoginCtrl', ['$state', 'AuthService', 
    function($state, AuthService) { 
     var self = this; 

     self.submit = function() { 
     AuthService.login(self.credentials) 
      .then(function(res) { 
      console.log('success'); 
      $state.go('home'); 
      }, function(res) { 
      if (res.status === 400) { 
       console.log('error') 
      } 
      }); 
     }; 
    } 
    ]); 

login.service.js

angular.module('exampleModule') 
    .factory('AuthService', ['$http', 
    function($http) { 
     var authService = {}; 

     authService.login = function(credentials) { 
     return $http.post('/api/authenticate', credentials); 
      .then(function(res) { 
      return res; 
      }); 
     }; 

     return authService; 
    } 
    ]); 

login.controller.test.js

describe('Controller: LoginCtrl', function() { 
    beforeEach(module('exampleModule')); 

    var ctrl, authService; 

    beforeEach(inject(function($controller, AuthService){ 
    ctrl = $controller('LoginCtrl'); 
    authService = AuthService; 
    })); 


    describe('submit function', function() { 
    beforeEach(function(){ 
     ctrl.submit(); 
    }); 

    it('should call AuthService', function() { 
     expect(authService.login).toHaveBeenCalled(); 
    });  
    }); 

}); 

如何正确测试AuthService.login是否被调用?有了办法,我注入AuthService到我的测试,我得到这些错误:

TypeError: 'undefined' is not an object (evaluating 'AuthService.login(self.credentials).then') 

回答

1

您需要模拟login()方法,并使其返回一个承诺:

describe('Controller: LoginCtrl', function() { 
    beforeEach(module('exampleModule')); 

    var ctrl, authService, $q; 

    beforeEach(inject(function($controller, _$q_, AuthService){ 
    ctrl = $controller('LoginCtrl'); 
    $q = _$q_; 
    authService = AuthService; 
    })); 


    describe('submit function', function() { 
    beforeEach(function(){ 
     var deferred = $q.defer(); 
     spyOn(authService, 'login').and.returnValue(deferred.promise); 
     ctrl.submit(); 
    }); 

    it('should call AuthService', function() {  
     expect(authService.login).toHaveBeenCalled(); 
    });  
    }); 
}); 

Working Plunker

相关问题