gpt4 book ai didi

angular - 单元测试 HttpClientModule 时出错(Angular 4.3+)

转载 作者:太空狗 更新时间:2023-10-29 18:19:58 27 4
gpt4 key购买 nike

我正在探索新的 Angular HttpClientModule 并遇到莫名其妙的错误。该模块足够新,我还找不到任何关于如何进行单元测试的有用信息,而且官方文档也没有任何示例。

该应用程序包含一个具有一种方法的服务,该方法将 URL 传递给 http.get。当我在浏览器上下文(又名 ng serve)中调用此方法时,http 调用正常执行。在单元测试的上下文中调用时,出现以下错误:

TypeError:您在需要流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。

这是一个使用 Angular CLI 生成的最小应用程序。这是相关代码:

app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppComponent } from './app.component';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, HttpClientModule],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts:

import { Component, OnInit } from '@angular/core';
import { TestService } from './test.service'

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [TestService]
})
export class AppComponent {
title = 'app';

constructor(private testSvc: TestService) {}

ngOnInit() {
this.testSvc.executeGet('http://www.example.com');
}
}

test.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class TestService {
constructor(private http:HttpClient) {}

executeGet(url:string) {
this.http.get(url)
.subscribe(
data => console.log('success', data),
error => console.log('error', error)
);
}
}

测试.service.spec.ts:

import { HttpClient, HttpHandler } from '@angular/common/http';
import { TestBed, inject } from '@angular/core/testing';
import { TestService } from './test.service';

describe('TestService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [HttpClient, HttpHandler, TestService]
});
});

it('should executeGet', inject([TestService], (svc: TestService) => {
expect(svc.executeGet).toBeDefined();
// this is where the 'undefined' error occurs
svc.executeGet('http://www.example.com');
}));
});

非常感谢任何指导或指示。

最佳答案

最近我遇到了完全相同的问题,错误消息说:


TypeError:您在需要流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。

对我来说,这个错误的根源是没有正确使用 HttpInterceptor。如果您还提供自定义 HttpInterceptor,请确保正确使用它。在下面的代码片段中,如果错误状态不是 401,请注意我是如何错过返回 Observable、Promise、Array 或 Iterable 的。默认情况下 undefined 拦截器返回 方法而不是 Observable、Promise、Array 或 Iterable,所以 Angular 对此表示提示。

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).catch(err => {
if(err instanceof HttpErrorResponse) {
if(err.status === 401) {
this.store.dispatch(this.authAction.unauthorized(err));
return Observable.throw(err);
}
}
})
}

修复方法是遵循代码片段。

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).catch(err => {
if(err instanceof HttpErrorResponse) {
if(err.status === 401) {
this.store.dispatch(this.authAction.unauthorized(err));
}
}
return Observable.throw(err); //Actually this line of code
})
}

关于angular - 单元测试 HttpClientModule 时出错(Angular 4.3+),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45326952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com