Angular路由单元测试失败
创始人
2024-10-28 01:34:54
0
  1. 确认导入的依赖是否正确,并在 TestBed 的配置中添加 RouterModule:

import { TestBed, async } from '@angular/core/testing'; import { RouterModule } from '@angular/router'; import { RouterTestingModule } from "@angular/router/testing"; import { AppComponent } from './app.component';

describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ RouterTestingModule, RouterModule ], declarations: [ AppComponent ], }).compileComponents(); })); });

  1. 在测试之前手动导入 AppRoutingModule,并添加一个 NgModuleRef 的 mocked 实例,以使测试通过:

import { Component, NgModule } from '@angular/core'; import { RouterModule } from '@angular/router'; import { AppRoutingModule } from './app-routing.module';

@Component({ selector: 'app-test-cmp', template: '', }) export class TestComponent {}

@NgModule({ imports: [AppRoutingModule, RouterModule], declarations: [TestComponent], exports: [TestComponent], }) export class TestModule {}

describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [RouterTestingModule.withRoutes([]), TestModule], declarations: [AppComponent], }) .overrideComponent(AppComponent, { set: { providers: [{ provide: NgModuleRef, useValue: {} }], }, }) .compileComponents(); })); });

  1. 在 TestBed 配置中将 RouterTestingModule 的 imports 与 AppRoutingModule 合并,以使测试通过:

TestBed.configureTestingModule({ imports: [RouterTestingModule.withRoutes(routes), AppRoutingModule], declarations: [AppComponent, HomeComponent, AboutComponent], });

通过以上步骤进行配置后,用 Karma 运行测试即可。

相关内容

热门资讯

第三分钟演示!wepoker正... 第三分钟演示!wepoker正确养号方法(透视)一直真的是有辅助安装(哔哩哔哩)1、让任何用户在无需...
1分钟手段!hhpoker免费... 1分钟手段!hhpoker免费透视脚本(透视)好像存在有辅助工具(哔哩哔哩)1、任何hhpoker免...
第3分钟模板!wejoker辅... 第3分钟模板!wejoker辅助器怎么卖(透视)原来存在有辅助教程(哔哩哔哩)运wejoker辅助器...
一分钟大纲!wepoker私人... 一分钟大纲!wepoker私人局辅助(透视)总是存在有辅助工具(哔哩哔哩)1、金币登录送、破产送、升...
第八分钟窍要!德普之星有辅助软... 第八分钟窍要!德普之星有辅助软件吗(透视)切实有辅助透视(哔哩哔哩)该软件可以轻松地帮助玩家将德普之...
第4分钟手册!wepoker好... 第4分钟手册!wepoker好友房开挂(透视)竟然是有辅助安装(哔哩哔哩)wepoker好友房开挂能...
2分钟指南书!wepoker养... 2分钟指南书!wepoker养号规律(透视)真是有辅助透视(哔哩哔哩)wepoker养号规律辅助器是...
九分钟方针!wepoker俱乐... 九分钟方针!wepoker俱乐部辅助(透视)好像真的有辅助脚本(哔哩哔哩)1、起透看视 wepoke...
8分钟妙招!德州局脚本(透视)... 8分钟妙招!德州局脚本(透视)都是是真的有辅助安装(哔哩哔哩)德州局脚本辅助器是一种具有地方特色的麻...
两分钟大纲!hhpoker脚本... 两分钟大纲!hhpoker脚本下载(透视)一直真的是有辅助开挂(哔哩哔哩)1、hhpoker脚本下载...