gpt4 book ai didi

javascript - 未找到 Angular 返回路线上的 Jasmine 单元测试

转载 作者:行者123 更新时间:2023-12-02 21:16:16 25 4
gpt4 key购买 nike

我最近升级到 Angular 9.1.0-rc.2,一切看起来都在正常工作。我能够在本地和服务器上很好地提供应用程序,但是在运行 jasmine 单元测试时,我在每次测试的随机组件上都会收到以下错误:

    Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'not-found'
Error: Cannot match any routes. URL Segment: 'not-found'
at ApplyRedirects.noMatchError (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/@angular/router/fesm2015/router.js:4327:1)
at CatchSubscriber.selector (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/@angular/router/fesm2015/router.js:4291:1)
at CatchSubscriber.error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/operators/catchError.js:29:1)
at MapSubscriber._error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/Subscriber.js:75:1)
at MapSubscriber.error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/Subscriber.js:55:1)
at MapSubscriber._error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/Subscriber.js:75:1)
at MapSubscriber.error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/Subscriber.js:55:1)
at MapSubscriber._error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/Subscriber.js:75:1)
at MapSubscriber.error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/Subscriber.js:55:1)
at ThrowIfEmptySubscriber._error (http://localhost:9876/Users/asithadesilva/Sites/my-project/node_modules/rxjs/_esm2015/internal/Subscriber.js:75:1)
error properties: Object({ rejection: Error: Cannot match any routes. URL Segment: 'not-found', promise: [object Promise], zone: Zone({ _parent: Zone({ _parent: Zone({ _parent: Zone({ _parent: null, _name: '<root>', _properties: Object({ }), _zoneDelegate: ZoneDelegate({ _taskCounts: Object({ microTask: 0, macroTask: 0, eventTask: 0 }), zone: <circular reference: Object>, _parentDelegate: null, _forkZS: null, _forkDlgt: null, _forkCurrZone: null, _interceptZS: null, _interceptDlgt: null, _interceptCurrZone: null, _invokeZS: null, _invokeDlgt: null, _invokeCurrZone: null, _handleErrorZS: null, _handleErrorDlgt: null, _handleErrorCurrZone: null, _scheduleTaskZS: null, _scheduleTaskDlgt: null, _scheduleTaskCurrZone: null, _invokeTaskZS: null, _invokeTaskDlgt: null, _invokeTaskCurrZone: null, _cancelTaskZS: null, _cancelTaskDlgt: null, _cancelTaskCurrZone: null, _hasTaskZS: null, _hasTaskDlgt: null, _hasTaskDlgtOwner: null, _hasTaskCurrZone: null }) }), _name: 'ProxyZone', _properties: Object({ ProxyZo ...

这是我设置路线的方法:app-routing.module.ts

import { MetaGuard } from '@ngx-meta/core';
import { UserModule } from './modules/user/user.module';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { LoggedInGuard } from 'ngx-auth-firebaseui';

import { AdminGuard } from '@shared/guards/admin.guard';
import { CourseModule } from './modules/course/course.module';
import { AdminModule } from './modules/admin/admin.module';
import { PagesModule } from './pages/pages.module';

const routes: Routes = [
{
path: '',
loadChildren: async (): Promise<PagesModule> =>
import('./pages/pages.module').then((m: { PagesModule: PagesModule }) => m.PagesModule)
},
{
canActivateChild: [MetaGuard],
path: 'admin',
loadChildren: async (): Promise<AdminModule> =>
import('./modules/admin/admin.module').then((m: { AdminModule: AdminModule }) => m.AdminModule),
canActivate: [LoggedInGuard, AdminGuard]
},
{
path: 'course',
loadChildren: async (): Promise<CourseModule> =>
import('./modules/course/course.module').then((m: { CourseModule: CourseModule }) => m.CourseModule),
data: {
title: 'Course'
}
},
{
path: '**',
redirectTo: '/not-found',
data: {
title: 'Page Not Found'
}
}
];

@NgModule({
imports: [
RouterModule.forRoot(routes, {
scrollPositionRestoration: 'enabled'
})
],
exports: [RouterModule]
})
export class AppRoutingModule {}

pages-routing.module.ts

import { NotFoundComponent } from './not-found/not-found.component';
import { HomeComponent } from './home/home.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'not-found',
component: NotFoundComponent
}
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class PagesRoutingModule {}

app.module.ts

@NgModule({
declarations: [AppComponent],
imports: [
CommonModule,
BrowserModule,
BrowserAnimationsModule,
AppRoutingModule,
HttpClientModule,
BrowserAnimationsModule,
MetaModule.forRoot({
provide: MetaLoader,
useFactory: metaFactory
}),
FirebaseModule,
SharedModule,
Angulartics2Module.forRoot({
gst: {
trackingIds: [environment.googleAnalytics]
}
})
],
bootstrap: [AppComponent]
})

在实际调用该路由的组件中:this.router.navigate(['not-found']);

我尝试过的事情:

  • 删除路由中的异步调用
  • LazyLoad 子项:loadChildren:'./modules/course/course.module#CourseModule'
  • 设置this.router.navigate(['/not-found'])
  • 直接在主app-routing.module.ts中加载NotFoundComponent
  • 当我注释掉使用该路由的任何组件时,单元测试工作正常。

请注意,当我提供应用程序时,我能够访问未找到的页面。我有点卡在这个问题上,并且希望能得到任何有关可能导致此问题的原因的提示。

最佳答案

由于这里的答案,我似乎能够解决这个问题:https://stackoverflow.com/a/45315961/1432248

  beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([{ path: 'not-found', component: NotFoundComponent }]),
...,
],
declarations: [..., NotFoundComponent],
}).compileComponents();
}));

编辑 RouterTestingModule 并使用 withRoutes 并添加缺少的路由的路径和组件。原来的答案详细解释了为什么会发生这种情况。

关于javascript - 未找到 Angular 返回路线上的 Jasmine 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60961614/

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