2016-05-04 6 views
0

Ich benutze Angular App und ich habe Service "AuthenticationService", um Benutzerauthentifizierungsdaten über Controller auf Server zu buchen. Ich bin Anfänger des Jasmine-Unit-Tests. Ich kann nicht verstehen, warum dieses Problem auftaucht.Komponententests mit Jasmine für Login-Authentifizierung auf Post-Credential-Problem

error: cannot read Property 'andReturn' of 'Undefined;

Vielleicht habe ich wegen ein paar Daten post.

Mein Controller-Code ist:

angular.module('webApp') 
.controller('LoginController', ['$rootScope', '$scope', 'AuthenticationService', function ($rootScope, $scope, AuthenticationService) { 
    $scope.user = {}; 
    $scope.errors = {}; 
    $scope.authenticationError = false; 
    $scope.rememberMe = true; 

    $scope.login = function (event) { 

     event.preventDefault(); 
     AuthenticationService.Login({ // service called $http.post to send credentials. 
            username: $scope.username, 
            password: $scope.password, 
            rememberMe: $scope.rememberMe 
     },function(response){ 

      if(response.userName==undefined){ 
       $scope.authenticationError = true; 
      }else{ 

       AuthenticationService.SetCredentials(response.userName,response.createToken); 
      } 

     }); 
    }; 
}]); 

Mein Service-Code ist:

angular.module('webApp').service('AuthenticationService',function($http,$cookieStore,$rootScope,$q,$location,$timeout){ 

'use strict'; 
this.Login =function(credentials, callback){ 
    console.log(credentials); 
    var usernameData=credentials.username; 
    var passwordData=credentials.password; 
    var data1={"userName":usernameData,"password":passwordData} 

    var deferred = $q.defer(); 

    $http.post('authenticate', data1).success(function (response) { 

    callback(response); 
    console.log(deferred.resolve(response)); 
     }).error(function(error){ 

     deferred.resolve(error); 
     callback(error); 
    }); 

    return deferred.promise; 
} 
}); 

und mein Testcode ist:

describe('testController', function() { 
    var $controller, $scope, AuthenticationService; 
    var dt = {username: "user1", password: "user1", rememberMe: true}; 

    beforeEach(module('webApp', function($provide){ 
     AuthenticationService = jasmine.createSpyObj("AuthenticationService", ["Login"]); 

     AuthenticationService.Login.and.returnValue(dt); 
    })); 

    beforeEach(inject(function(_$controller_, $rootScope, _AuthenticationService_){ 
     $scope = $rootScope.$new(); 
     AuthenticationService = _AuthenticationService_; 
     $controller = _$controller_("LoginController", { 
      $scope : $scope, 
      AuthenticationService : AuthenticationService 
     }) 
    })); 

    it("should load the service", function(){ 
     expect(AuthenticationService.Login).toHaveBeenCalled(); 
    }) 
}); 

Antwort

1

AuthenticationService Dienst nicht verspottet, lokale Variable wird durch echten Dienst überschrieben:

AuthenticationService = _AuthenticationService_; 

Es kann es funktionierte mit

beforeEach(module('webApp', { 
    AuthenticationService : { Login: jasmine.createSpy().and.returnValue(dt) } 
})); 
+0

Dank spotten !! –