Uncaught Error: Uncaught (in promise): TypeError: this.xxxService.getSomething(...).toPromise is not a function
Uncaught Error: Uncaught (in promise): TypeError: this.xxxService.getSomething(...).toPromise is not a function
在组件中:
private xxxService: XxxService,
const xxxList = await this.xxxService.getSomething(this.someid).toPromise();
在测试文件中component.spec.ts:
import {Observable, of} from 'rxjs';
const xxxServiceStub = {
getSomething: () => Promise.resolve([])
};
beforeEach((async(next) => {
await TestBed.configureTestingModule({
declarations: [someComponent],
imports: [
....
],
providers: [
{provide: XxxService, useValue: xxxServiceStub},
]
}).compileComponents();
next();
}));
API:
public getSomething(someid): Observable<xxxRef[]> {
return this.httpClient
.get<xxxRef[]>(
${environment.url}/api/xxx?someid=${someid},
{
observe: 'response'
}
).pipe(
map(response => {
return response.body;
})
);
}
接口:
export interface xxxRef {
name: string;
id: number;
description?: string;
}
测试成功,但 Karma 控制台出现错误:
zone-evergreen.js:798 Uncaught Error: Uncaught (in promise): TypeError: this.xxxService.getSomething(...).toPromise 不是函数
您需要 return 来自 getSomething
方法的 Observable,但您 returning Promise.resolve
而不是 Observable,这就是您出错的原因。
将您的模拟方法更改为此
import { of } from 'rxjs';
...
const xxxServiceStub = {
getSomething: () => of([]) // this returns observable
};
mock toPromise
如果不知道 xxxServiceStub
是什么,很难告诉您确切的问题,但是,这可能是因为您没有返回 Observable
(具有 .toPromise
函数
您伪造的 xxxService.getSomething
需要看起来像这样才能在规范中工作:
getSomething(id: number): Observable<any> {
return of(true);
}
另一种方法是使用间谍:
spyOn(xxxService, 'getSomething').and.callFake(() => of(true));
在组件中:
private xxxService: XxxService,
const xxxList = await this.xxxService.getSomething(this.someid).toPromise();
在测试文件中component.spec.ts:
import {Observable, of} from 'rxjs';
const xxxServiceStub = {
getSomething: () => Promise.resolve([])
};
beforeEach((async(next) => {
await TestBed.configureTestingModule({
declarations: [someComponent],
imports: [
....
],
providers: [
{provide: XxxService, useValue: xxxServiceStub},
]
}).compileComponents();
next();
}));
API:
public getSomething(someid): Observable<xxxRef[]> {
return this.httpClient
.get<xxxRef[]>(
${environment.url}/api/xxx?someid=${someid},
{
observe: 'response'
}
).pipe(
map(response => {
return response.body;
})
);
}
接口:
export interface xxxRef {
name: string;
id: number;
description?: string;
}
测试成功,但 Karma 控制台出现错误:
zone-evergreen.js:798 Uncaught Error: Uncaught (in promise): TypeError: this.xxxService.getSomething(...).toPromise 不是函数
您需要 return 来自 getSomething
方法的 Observable,但您 returning Promise.resolve
而不是 Observable,这就是您出错的原因。
将您的模拟方法更改为此
import { of } from 'rxjs';
...
const xxxServiceStub = {
getSomething: () => of([]) // this returns observable
};
mock toPromise
如果不知道 xxxServiceStub
是什么,很难告诉您确切的问题,但是,这可能是因为您没有返回 Observable
(具有 .toPromise
函数
您伪造的 xxxService.getSomething
需要看起来像这样才能在规范中工作:
getSomething(id: number): Observable<any> {
return of(true);
}
另一种方法是使用间谍:
spyOn(xxxService, 'getSomething').and.callFake(() => of(true));