【发布时间】:2018-01-09 09:11:16
【问题描述】:
我通常使用 fakeAsync 来测试返回 observable 的订阅。但在这种罕见的情况下,我需要做同样的事情,只是为了一个承诺。
这是我的尝试:
//Service Stub:
const testService = {
testMethod:
jasmine.createSpy('testMethod').and.callFake(() => new Promise(() => 'test'))
};
it('test example',() => {
// Arrange
const response = 'test';
component.selected = undefined;
// Act
component['myMethod']();
//Assert
expect(component.selected).toEqual(response);
});
这是我的实际代码:
private myMethod(): void {
return this.testService.testMethod()
.then((response: string) => {
this.selected = response;
})
.catch(error => this.logger.error(error, this));
}
基本上,需要知道如何等待“this.testService.testMethod”返回并设置选中。
目前,它不等待承诺返回。
注意:我已经更新了我当前的尝试。但在期望中仍然未定义。
【问题讨论】:
-
我已经更新了上面的尝试 - 我仍然不确定
标签: angular unit-testing jasmine karma-jasmine