【发布时间】:2020-05-23 08:26:00
【问题描述】:
我需要在我的测试中进行 HTTP 调用,并且我需要验证我的可观察性。
当我调试代码时,HTTP 调用在服务中被调用,但在我的测试中,它失败并说 http 是 undefined,但在调试时我能够在控制台中看到 http 观察结果。
//service.ts file
//imports modles goes here
export class DataService {
private api = this.configService.config.apiUrl;
constructor(private http: HttpClient,
private configService: AppConfigService,
private errorService: ErrorService)
{
}
public getCustomerList(): Observable<IUserResponse> {
return this.http.get<IUserResponse>`${this.api}/v1/getusers/users`);
}
}
my test file serviec.specs.ts
describe('OnboardingService', () => {
beforeEach(() => {
const httpClient = new HttpClient(new HttpXhrBackend({ build: () => new XMLHttpRequest() }));
appConfigService= new AppConfigService(httpClient);
appConfigService.config = { apiUrl:"https://test-test-getuser.json"}
erroService = new ErrorService();
service = new Dataservice(httpClient, appConfigService,erroService);
it('should return an Observable<Iuser[]>', done => {
service.getCustomerList().subscribe(response => {
expect(response.users).toBeGreaterThan(0);
done();
});
});
})
【问题讨论】:
-
我猜this.http.get('{this.apiurl}/v1/getuser')有问题,应该是this.http.get('${this.apiurl} /v1/getuser')
-
你有外部日志语句吗?喜欢
console.log(this.http.get('{this.apiurl}/v1/getuser').subscribe((response)=>{console.log(response.user)}))?您需要显示更多代码。请发布进行此调用的函数。 -
你能执行这一行 this.http.get('{this.apiurl}/v1/getuser').subscribe(console.log) 并发送控制台打印的内容吗?
-
需要进一步了解该问题以帮助解决它
标签: javascript node.js angular angular6