TypeScript / Angular $ q立即解决延迟无法正常工作

时间:2015-10-28 16:05:02

标签: javascript angularjs typescript angular-promise

我有一个我在TypeScript中创建的Angular Service类,我这个服务有一个load方法。这个特殊的服务,它加载的列表实际上是硬编码的,所以我不需要从任何后端服务加载它。我希望load方法返回一个promise,因为我想服务看起来像我在课堂上的其他数据服务。

这是我拥有的数据服务

module MyApplication.Data {

    export interface IDestinationService {

        load(): ng.IPromise<Array<MyApplication.Models.Destination>>;

    }

    export class DestinationService implements IDestinationService {

        items: Array<MyApplication.Models.Destination>;

        constructor($http: ng.IHttpService, private $q: ng.IQService) {
            this.items = new Array<MyApplication.Models.Destination>();
            this.items.push(new MyApplication.Models.Destination());
            this.items.push(new MyApplication.Models.Destination());
            this.items[0].Id = 2;
            this.items[0].Description = 'Item 1';
            this.items[1].Id = 3;
            this.items[1].Description = 'Item 2';
        }

        load(): ng.IPromise<Array<MyApplication.Models.Destination>> {
            var defer = this.$q.defer();

            defer.resolve(this.items);

            return defer.promise;
        }

    }

}

根据我的阅读,这应该使服务工作。它将返回一个promise,但是当它返回时,promise会立即得到解决,因此then方法应该触发。

我有一个Jasmine测试类,如下所示:

module MyApplication.Tests {

    describe('Data', () => {

        describe('Destination', () => {

            var $http: ng.IHttpService;
            var $httpBackend: ng.IHttpBackendService;
            var $q: ng.IQService;

            beforeEach(inject((_$http_: ng.IHttpService, _$httpBackend_: ng.IHttpBackendService, _$q_: ng.IQService) => {
                $http = _$http_;
                $httpBackend = _$httpBackend_;
                $q = _$q_;
            }));

            describe('', () => {
                var results: Array<MyApplication.Models.Destination>;

                beforeEach((done) => {
                    var service = new MyApplication.Data.DestinationService($http, $q);
                    service.load()
                        .then((result) => {
                            results = result;
                            done();
                        });
                });

                it('Returns Planning Brokers list', () => {
                    expect(results.length).toBe(2);
                });

            });

        });

    });

}

但是当我运行此测试时,我从Jasmine收到Async超时错误,因为then方法永远不会触发。我怎样才能让它正常工作。

1 个答案:

答案 0 :(得分:1)

您不应该需要第二个describebeforeEach块。使用rootScope.$digest解决承诺并重构您的测试代码,如下所示:

describe('Data', () => {

    describe('Destination', () => {

        var $http: ng.IHttpService;
        var $httpBackend: ng.IHttpBackendService;
        var $q: ng.IQService;

        beforeEach(inject((_$http_: ng.IHttpService, _$httpBackend_: ng.IHttpBackendService, _$q_: ng.IQService) => {
            $http = _$http_;
            $httpBackend = _$httpBackend_;
            $q = _$q_;
        }));

        it('Returns Planning Brokers list', () => {
            var results: Array<MyApplication.Models.Destination>;

            var service = new MyApplication.Data.DestinationService($http, $q);
            service.load().then((results) => {
                expect(results.length).toBe(2);
            });

            $rootScope.$digest();
        });

    });

});