单元测试Angular 1(带打字稿)控制器

时间:2017-06-11 14:49:34

标签: angularjs unit-testing typescript jasmine karma-jasmine

我使用typescript在Angular 1中有一个小项目,我想使用jasmine进行一些单元测试。到目前为止很酷!首先,我想测试我的控制器,以确保当我从那里调用一个方法时,我会得到正确的响应。很简单,嗯?!

我已经有了控制器实例,我可以访问一些范围变量值。但是,当我尝试调用一个方法时,它不起作用(我没有为该方法定义)。请参阅以下代码:

找到控制器:

/// <reference path="../../app.d.ts" />

module UnitTest {

    export class LocateController {

        private myLocation: any;
        private myLocationBkp: any;
        private now: Date;

        private searchForm: any;
        private urlRegex: any;

        constructor(private $scope: ng.IScope, 
            private $http: ng.IHttpService, 
            private $q: ng.IQService, 
            private $translate: ng.translate.ITranslateService,
            private locationService: LocationService) {

                this.myLocation = {};
        }

        /* Method responsible to return all information about the user. */
        public getMyLocation() {

            this.now = new Date();

            this.locationService.getMyLocation().then((data) => {
                this.myLocation = data;       
            });
        }

    }
}
app.controller('LocateController', ['$scope', '$http', '$q', '$translate', 'LocationService', UnitTest.LocateController]);

找到服务

/// <reference path="../../app.d.ts" />

module UnitTest {

    export class LocationService {

        /* ---- global variables ---- */
        private API_HOST: string = "http://freegeoip.net";

        constructor(private $http: ng.IHttpService,
            private $q: ng.IQService) {

        }


        /* Method responsible to retrieve user's location information  */
        public getMyLocation(): ng.IPromise<any> {

            var defer = this.$q.defer();
            $.ajax({
                type : 'GET'
               ,url : this.API_HOST + '/json/'

               ,success: (response) => {defer.resolve(response);}
               ,error: (error) => {defer.reject('Something went wrong: ' + error.responseText);} 
            });

            return defer.promise;
        }
    }
}

app.service("LocationService", ['$http', '$q', UnitTest.LocationService]);

控制器规格

/// <reference path="../app.d.ts" />

describe('Locate Controller', () => {
    var $rootScope;
    var scope;
    var $httpBackend;
    var ctrl = null;
    var locService;

    beforeEach(angular.mock.module('unittest'));
    beforeEach(inject((_$httpBackend_, $rootScope, _$controller_, LocationService) => {
        $httpBackend = _$httpBackend_;
        scope = $rootScope.$new();
        locService = LocationService;
    }));

    it('should call getMyLocation method and change myLocation value', inject(($controller) => {
        ctrl = $controller('LocateController', {$scope: scope});
        ctrl.getMyLocation();
        expect(ctrl.myLocation).toEqual(''); // empty is the initial value. After calling the method it should be different of ''
    }));

});

在Chrome中调试我的规范测试代码,当我正在检查对方法的调用时,这是显示的内容: see screenshot

why the method does not appear here?

我不确定方法是否被正确调用但是我没有等待promise响应或者没有被正确调用。因此,请帮助我使它成功!

如果需要任何其他信息,请告诉我们!

0 个答案:

没有答案