Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 单元测试角度1(带typescript)控制器_Angularjs_Unit Testing_Typescript_Jasmine_Karma Jasmine - Fatal编程技术网

Angularjs 单元测试角度1(带typescript)控制器

Angularjs 单元测试角度1(带typescript)控制器,angularjs,unit-testing,typescript,jasmine,karma-jasmine,Angularjs,Unit Testing,Typescript,Jasmine,Karma Jasmine,我在Angular 1中有一个使用typescript的小项目,我想使用jasmine进行一些单元测试。酷到目前为止!首先,我想测试我的控制器,以确保当我从那里调用一个方法时,我会得到正确的响应。很简单吧 我已经得到了控制器实例,并且能够访问一些范围变量值。但是,当我尝试调用一个方法时,它不起作用(我没有为该方法定义)。见以下代码: 定位控制器: // 模块单元测试{ 导出类LocateController{ 私人位置:任何; 私人myLocationBkp:任何; 现在列兵:日期; 私人搜索形

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

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

定位控制器:
//
模块单元测试{
导出类LocateController{
私人位置:任何;
私人myLocationBkp:任何;
现在列兵:日期;
私人搜索形式:任何;
私有urlRegex:任何;
构造函数(私有$scope:ng.IScope,
私有$http:ng.IHttpService,
私人$q:ng.IQ服务,
私人$translate:ng.translate.ITranslateService,
专用定位服务:定位服务){
this.myLocation={};
}
/*方法负责返回有关用户的所有信息*/
公共getMyLocation(){
this.now=新日期();
this.locationService.getMyLocation()。然后((数据)=>{
this.myLocation=数据;
});
}
}
}
app.controller('LocateController',['scope','http','q','translate','LocationService',UnitTest.LocateController]);
定位服务
//
模块单元测试{
出口类定位服务{
/*----全局变量--*/
私有API_主机:字符串=”http://freegeoip.net";
构造函数(私有$http:ng.IHttpService,
私人$q:ng.IQService){
}
/*负责检索用户位置信息的方法*/
public getMyLocation():ng.IPromise

我不确定该方法是否被正确调用,但我没有等待承诺响应,或者该方法没有被正确调用。因此,请帮助我使其正常工作


如果需要任何附加信息,请让我知道!< /p>因为该方法是异步的,它确实是HTTP请求,这是单元测试中的“否”。没有足够的理由在角上使用$Ajax。考虑用$HTTP替换它,然后按照手册建议测试它。嗨,ESTUS!谢谢您回复我!我已经将$Ajax更改为$HTTP。.现在,在达到预期条件之前,我如何调用该方法并等待promise响应?因为ctrl.myLocation变量根本没有按预期更改其值。现在,应使用$http请求进行模拟,并同步执行该承诺。

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

});