【发布时间】:2018-01-18 06:01:03
【问题描述】:
我之前已经成功测试了一个使用 ES7 async/await 语法和 jasmine 的角度控制器 -
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();
});
有几点需要指出——
- 在使用异步等待之前,此测试已通过。现在,我没有通过服务中的等待。
- 该功能在不在测试环境中时有效。
- 我正在使用 jasmine 2.4.1 和 angularJS 1.6
【问题讨论】:
-
你有没有找到任何通过异步等待通过测试的解决方案?
标签: angularjs jasmine ecmascript-2017