使用Jasmine angular js测试真正的https后调用

时间:2017-03-04 08:08:52

标签: angularjs unit-testing https jasmine

我有以下服务和控制器将数据插入数据库。模块应用程序已经定义,没有Jasmine一切正常。

app.service('registerService', function($http, $q) {
    var deferred = $q.defer();
    this.register = function(userDetails) {
        /* TODO: Populate the userId in the fly */
        var dataPart = {
            userName : userDetails.userName,
            firstName : userDetails.firstName,
            lastName : userDetails.lastName,
            password : userDetails.password,
            email : userDetails.email,
            phone : userDetails.phone
        };
        return $http.post('/phase1/blogger/user/addUser/', dataPart)
            .then(function(response) {
                deferred.resolve(response.data);
                return deferred.promise;
            }, function(response) {
                // the following line rejects the promise 
                deferred.reject(response);
                return deferred.promise;
            });
    };
});

以下是控制器。

app.controller('registerController', function($scope, $location, registerService, validationService) {
    $scope.newUserData = '';
    $scope.temp = '';
    $scope.registerUser = function(userDetails) {       
        $scope.error = '';
        if (userDetails != null && userDetails.firstName != null && userDetails.userName != null &&
                userDetails.password != null && userDetails.email != null && userDetails.phone != null) {       
            if (validationService.validatePhone(userDetails.phone)) {
                    // Validate Password and Confirm Password 
                if (validationService.validatePassword(userDetails.password, userDetails.confirmPassword)) {
                    $scope.temp = 'dee2';
                    var c = JSON.parse(JSON.stringify(userDetails));
                    $scope.temp = 'after stringify';
                    registerService.register(c)
                        .then(function(data) {
                            $scope.newUserData = data;
                            $scope.temp = 'dee3';
                            $location.path('/');
                        }, function(error) {
                            $scope.temp = 'dee3';
                            alert("Registration Failed");
                            $location.path('/');
                        });
                } else {
                    $scope.error = 'Password and Confirm Password donot match';
                }
            } else {
                $scope.error = 'Enter a valid Phone number';
            }
        } else {
            $scope.error = 'Mandatory field UserName or FirstName or Password or Email or Phone empty';
        }
    };
    $scope.registerCancel = function() {
        $location.path('/');
    };

});

以下是JASMINE测试代码。

describe('new App Test', function () {
    beforeEach(function() {
        module('newDirectiveApp');
        module('newApp');
    });

    var $controller;
    var $httpBackend;

    beforeEach(inject(function(_$controller_, _$httpBackend_) {
        $controller = _$controller_;
        $httpBackend = _$httpBackend_;
    }));

    describe('Test User Registration', function() {

        it("should register a new user", function() {
            var $scope = {};
            var controller = $controller('registerController', { $scope: $scope });

            var details = {};
            details.firstName = 'new1';
            details.lastName = 'new1Last';
            details.userName = 'new1';
            details.password = '123';
            details.confirmPassword = '123';
            details.email = 'new1@gmail.com';
            details.phone = '9986513765';
            $scope.registerUser(details);
            var dataPart =  {   
                "userName" : "new1",            
                "firstName" : "new1",
                "lastName" : "new1Last",
                "password" : "123",
                "email" : "new1@gmail.com",
                "phone" : "9986513765"
             };

            expect($scope.temp).toEqual('newDee');
        });
    });        
});

当$ scope.registerUser(详情);被称为是看到代码到达$ scope.temp ='在stringify&#39 ;; in" registerController"。 但它没有达到$ scope.temp =' dee3&#39 ;; 我可以知道原因吗? 提前谢谢。

0 个答案:

没有答案