1
我以前成功地测试了使用异步ES7/AWAIT语法与茉莉角控制器 -
async updateGridAsync() {
const paging = angular.copy(this.gridData.getServerCallObj());
}
try {
const model = await this.service.getAsync(paging);
this._$rootScope.$apply();
} catch (e){this._notification.error(this._$rootScope.lang.notifications.unexpectedError);
}
}
it('updateGridAsync() should update the gridData when succeed', async (done) => {
expect(ctrl.gridData.totalItems).toEqual(2);
expect(ctrl.gridData.items.length).toEqual(2);
spyOn(service, 'getAsync').and.callFake(() => {
return Promise.resolve(responseStub);
});
await ctrl.updateGridAsync();
expect(service.getAsync).toHaveBeenCalled();
expect(ctrl.gridData.totalItems).toEqual(1);
expect(ctrl.gridData.items.length).toEqual(1);
done();
});
上面的代码完美地工作。但是,一旦尝试测试调用$ http.post的模拟服务代码时,我遇到了一个问题。这是我在服务运行代码:
async getAsync(pagingData, spinner, gridId, payeeId){
const response = await $http.post(url, params);
const model = this.modelFactory(response.data);
return model ;
}
和测试方法,是不是能够在updateGridService的AWAIT后走了一步:
it('getAsync should return correct model', async (done) => {
$httpBackend.whenPOST(theUrl).respond(200, responseStub);
const model = await service.getAsync();
expect(model.list.length).toEqual(2);
$httpBackend.flush();
done();
});
有几件事情要指出 -
- 在使用异步等待之前,通过了该测试。现在,我不会在服务中等待。
- 当不在测试环境中时,该功能起作用。
- 我使用茉莉2.4.1和1.6 angularJS
您是否发现任何通过异步通过测试的解决方案? – Nader