TypeScript/Angular $q 立即解决延迟不起作用
TypeScript/Angular $q immediately resolve defer not working
我有一个用 TypeScript 创建的 Angular 服务 class,我这个服务有一个加载方法。它加载的这个特定服务列表实际上是硬编码的,所以我不需要从任何后端服务加载它。我希望加载方法 return 是一个承诺,因为我希望服务看起来像我在 class.
中拥有的其他数据服务
这是我的数据服务
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;
}
}
}
据我所知,这应该可以使服务正常运行。它将 return 一个承诺,但承诺将在 returned 时立即解决,因此应该触发 then 方法。
我有一个 Jasmine 测试 class,如下所示:
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 收到异步超时错误,因为 then 方法永远不会触发。我怎样才能让它正常工作。
您不需要第二个 describe
和 beforeEach
块。使用 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();
});
});
});
我有一个用 TypeScript 创建的 Angular 服务 class,我这个服务有一个加载方法。它加载的这个特定服务列表实际上是硬编码的,所以我不需要从任何后端服务加载它。我希望加载方法 return 是一个承诺,因为我希望服务看起来像我在 class.
中拥有的其他数据服务这是我的数据服务
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;
}
}
}
据我所知,这应该可以使服务正常运行。它将 return 一个承诺,但承诺将在 returned 时立即解决,因此应该触发 then 方法。
我有一个 Jasmine 测试 class,如下所示:
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 收到异步超时错误,因为 then 方法永远不会触发。我怎样才能让它正常工作。
您不需要第二个 describe
和 beforeEach
块。使用 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();
});
});
});