angular - 使用 Angular cli 进行单元测试,http 测试不起作用
问题描述
专辑.spec.ts
describe("AlbumsComponent", () => {
let component: AlbumsComponent;
let fixture: ComponentFixture<AlbumsComponent>;
let albumService: AlbumService;
let httpMock: HttpTestingController;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
......
],
imports: [AppRoutingModule, HttpClientTestingModule],
providers: [AlbumService, HttpTestingController]
}).compileComponents();
albumService = TestBed.get(AlbumService);
httpMock = TestBed.get(HttpTestingController);
}));
beforeEach(() => {
fixture = TestBed.createComponent(AlbumsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it("should retrieve posts from the API via GET", () => {
const dummyAlbums: Album[] = [
{
userId: 1,
userName: "Arpan",
id: 1,
title: "quidem molestiae enim"
},
{
userId: 1,
userName: "Banerjee",
id: 2,
title: "sunt qui excepturi placeat culpa"
}
];
albumService.fetchAlbums().subscribe(albums => {
expect(albums.length).toBe(2);
expect(albums).toEqual(dummyAlbums);
});
const request = httpMock.expectOne(
"http://jsonplaceholder.typicode.com/albums"
);
expect(request.request.method).toBe("GET");
request.flush(dummyAlbums);
});
});
我收到以下错误-
10 specs, 1 failure, randomized with seed 94637
Spec List | Failures
AlbumsComponent > should retrieve posts from the API via GET
**TypeError: httpMock.expectOne is not a function**
at <Jasmine>
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/albums/albums.component.spec.ts:67:30)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:359:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:308:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:358:1)
at Zone.run (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-evergreen.js:124:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:561:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/node_modules/zone.js/dist/zone-testing.js:576:1)
at <Jasmine>
没有编译错误。这是我的服务班
专辑.service.ts
@Injectable({ providedIn: "root" })
export class AlbumService {
constructor(private http: HttpClient) {}
albumUrl = "http://jsonplaceholder.typicode.com/albums";
//get the album title along with the user name
**fetchAlbums(): Observable<any> {
return this.http.get<Album[]>(this.albumUrl).pipe(
tap(albums => {
albums.map((album: { userId: String; userName: String }) => {
this.fetchUsers(album.userId).subscribe((user: any) => {
album.userName = user[0].username;
});
});
// console.log(albums);
})
);
}**
TypeError:httpMock.expectOne 不是函数
我无法理解为什么 expectOne 不是函数。我是角度新手,我正在学习角度测试。这是我关注的 youtube 视频的链接https://www.youtube.com/watch?v=4JVnSkR04tM&t=376s
解决方案
推荐阅读
- codeigniter-4 - Codeigniter4 - 找不到控制器或其方法:\App\Controllers\Startup::index
- opencv - 比较 F 矩阵和 E 矩阵
- c# - 为什么变量在我赋值后会重置其原始值?
- c - GtkTextIter 和 GtkTextMark 有什么关系?
- flutter - 我无法为您的应用添加耀斑闪屏
- javascript - 如何捕获自定义事件?
- python - 组合值为 1 的 python 列表元素加上偏移量(屏蔽)
- javascript - 使用 MongoDB + Express 的“未定义”对象
- python - 错误:从 Google Coblab 中的 osmnx folium 加载背景地图时出现“localhost 拒绝连接”
- node.js - 根据登录状态快速路由到 React 应用程序