我需要在 Jasmin 中测试异步承诺,但我不知道该怎么做,因为 Angular 每次都会返回错误。
myService.ts
async getNewContext(): Promise<IContext> {
const test1 = await this.service1.function();
const test2 = await this.service2.function();
const test3 = await this.service3.function();
const test4 = await this.service4.function();
const cc = {
.
.
.
};
this.setContext = cc;
return cc;
}
组件.ts
setFeatures() {
this.myService.getNewContext().then((response) => {});
}
组件.spect.ts
.
.
.
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [...],
imports: [...],
providers: [
{
provide: myService,
useValue: {
select: () => [],
getNewContext: () => [],
},
}
],
}).compileComponents();
}));
我收到一条错误消息“getNewContext 不是函数”。请问有什么想法吗?
使函数返回一个promise,
then
仅在返回promise时才存在。
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [...],
imports: [...],
providers: [
{
provide: myService,
useValue: {
select: () => [],
getNewContext: () => Promise.resolve(1),
},
}
],
}).compileComponents();
}));