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 运行测试即可。

相关内容

热门资讯

透视了解!wepower有外挂... 透视了解!wepower有外挂,扑克世界app辅助,确实真的是有挂(详细辅助透视教程)1、下载好we...
透视了解!智星德州菠萝有挂吗,... 透视了解!智星德州菠萝有挂吗,来玩app辅助工具,真是是有挂(详细辅助新2025教程)1、打开软件启...
透视黑科技!WePoKe透视挂... 透视黑科技!WePoKe透视挂,wepoke有没有挂,其实有挂(详细辅助2025新版总结);WePo...
透视系统!德扑之星ai代打,w... 透视系统!德扑之星ai代打,wpk透视辅助封号吗,一直真的是有挂(详细辅助存在挂教程)1、德扑之星a...
透视插件!aapoker有外挂... 透视插件!aapoker有外挂,德州ai人工智能软件免费,其实真的有挂(详细辅助解密教程);透视插件...
透视了解!德扑之星有猫腻,we... 透视了解!德扑之星有猫腻,wepoke用模拟器有什么用,本来真的是有挂(详细辅助详细教程)进入游戏-...
透视智能ai!微扑克系统发牌规... 透视智能ai!微扑克系统发牌规律,hm3德州辅助,原来真的有挂(详细辅助必胜教程)1、每一步都需要思...
透视新版!微扑克ai机器人,f... 透视新版!微扑克ai机器人,fishpoker扑克辅助,原来真的有挂(详细辅助透牌教程);1、点击下...
透视最新!wepower辅助器... 透视最新!wepower辅助器,德州wpk德州真的,一直存在有挂(详细辅助揭秘攻略)1、玩家可以在w...
透视挂透视!wepoke计算辅... 透视挂透视!wepoke计算辅助,红龙扑克是真是假,真是真的是有挂(详细辅助安装教程)1、wepok...