1

I am new in jasmine and trying to write test case.. I am stuck need a little push/help. I am using JASEMINE in my MEAN app.

Here is code!! Service

angular.module('rugCoPro')

.service('AuthenticationService', ['$http', 'ObjectUtils', 'Session', function($http, ObjectUtils, session){



    var isError = function(data){
        return (ObjectUtils.isObject(data) && data.hasOwnProperty("error"));
    };
    /*
    *
    * login
    *
    */
    this.login = function(username, password, errCallBack, successCallBack){
        if(ObjectUtils.isNonEmptyString(username) && ObjectUtils.isNonEmptyString(password)){
            var authData = btoa(username + ":" + password);
            var headers = {Authorization:'Basic ' + authData};
            $http.get("api/staff/authenticate", {headers:headers})
                .then(

                    function onSuccess(response, status){
                        if(!ObjectUtils.isObject(response) || !ObjectUtils.isObject(response.data)){
                            if(errCallBack)errCallBack("Invalid server response, please try again");
                            return;
                        }

                        var resData = response.data;

                        if (isError(resData)){
                            if(ObjectUtils.isObject(resData) && ObjectUtils.isObject(resData.error)){
                                if(errCallBack)errCallBack(resData.error.message);
                            }
                        }else{
                            $http.defaults.headers.common["X-Auth-Token"] = resData._token;
                            session.setAuthenticated(true);
                            session.setAuthToken(resData._token);
                            session.setUser(resData.user);
                            if(successCallBack) successCallBack();
                        }
                    },
                    function onError(response, status){
                        if(ObjectUtils.isObject(response)){
                            var hdrs = response.headers();
                            if(ObjectUtils.isObject(hdrs) && ObjectUtils.isNonEmptyString(hdrs["www-authenticate"])){
                                if(errCallBack) errCallBack(hdrs["www-authenticate"]);
                            }else{
                                if(errCallBack) errCallBack("An authorization error occurred, invalid response header");
                            }
                        }else{
                            if(errCallBack) errCallBack("An authorization error occurred, invalid response");
                        }
                    }
                );
        }else{
            if(errCallBack) errCallBack("Please enter a valid username and password");
        }
    };
}]);

the test case i am writing is ...

describe("Authentication services spec", function () {
    var redditService, httpBackend;

    beforeEach(function() {
        module('rugCoPro');
    });

    it('should contain a Authentication Service',
       inject(function(AuthenticationService, $httpBackend) {
            expect(AuthenticationService).not.toBeUndefined();
    }));

}); 

I am trying to write some test cases for this service but I am not getting how can I develop fake call for errCallBack and successCallBack.

for more clear view: This is the how I am calling service from the controller. I will be super greatful for your help.

AuthenticationService.login($scope.username, $scope.password, 
            function(error){
                $scope.showLoginError(error);
            },
            function(){
                $state.go('staff');
            }
        );
yankee
  • 38,872
  • 15
  • 103
  • 162
jatinder bhola
  • 385
  • 1
  • 7
  • 23

0 Answers0