mercredi 4 mai 2016

unit testing with jasmine for login authentication on post credential issue

I am using angular app and I have service "AuthenticationService" to post user auth data to server via controller. I am beginner of jasmine unit test I Cannot understand why this issue comes up. error: cannot read Property 'andReturn' of 'Undefined; may be because of I have post some data...If anyone knows knows or already done such kind of testing please help me out..

My controller code is:::

  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);
            }

        });
    };
}]);

My service code is ::::

          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;
}
});

and my test code is ::::

           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();
})

});

Aucun commentaire:

Enregistrer un commentaire