[英]Failed: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'login'. Jasmine & Angular 6
Community I just starting to play with unit testing and I have set up an Angular 6 project with Jasmine & karma for unit testings. 社区我刚刚开始进行单元测试,我已经与Jasmine&karma建立了Angular 6项目以进行单元测试。
for some weird reason, I get this error when i RUN ng test
Jasmine opens new browser window on http://localhost:9876/?id=34428211
出于某些奇怪的原因,当我运行
ng test
Jasmine在http://localhost:9876/?id=34428211
上打开新的浏览器窗口时,出现此错误
Failed: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'login'
Error: Cannot match any routes. URL Segment: 'login'
I am on the grid-page.component.ts where I don't have any route that redirects to login page expect the authentication service that checks if the user is logged in and if the user token has expired and only then I redirect to the login page. 我在grid-page.component.ts上 ,那里没有任何重定向到登录页面的路由,期望身份验证服务检查用户是否已登录以及用户令牌是否已过期,然后才重定向到登录页面。 That's the only possible solution I can find can someone explain to me what I can do solve this.
那是我能找到的唯一可能的解决方案,有人可以向我解释我可以解决该问题的方法。
One more thing I want to ask you guys is if I need to make any test for the app-routing.module.ts because I don't test any routes. 我想问的一件事是,我是否需要对app-routing.module.ts进行任何测试,因为我不测试任何路由。
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NotificationsComponent } from '../../../components/ui/notifications/notifications.component';
import { MaterialModule } from '@app-modules/material.module.ts';
import { SidenavComponent } from '../../../components/ui/sidenav/sidenav.component';
import { HeaderComponent } from '../../../components/ui/header/header.component';
import { PlotToolbarComponent } from '../../../components/ui/plot-toolbar/plot-toolbar.component';
import { GridFiltersComponent } from '../../../pages/analytics/grid-page/components/grid-filters/grid-filters.component';
import { LoaderComponent } from '../../../components/loaders/loader/loader.component';
import { CalendarGridPlotComponent } from '../../../pages/analytics/grid-page/components/calendar-grid-plot/calendar-grid-plot.component';
import { RouterTestingModule } from '@angular/router/testing';
import { DropDownMenuComponent } from '../../../components/form_controls/drop-down-menu/drop-down-menu.component';
import { ReactiveFormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { GridPageComponent } from './grid-page.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { HttpClientModule } from '@angular/common/http';
describe('GridPageComponent', () => {
let component: GridPageComponent;
let fixture: ComponentFixture<GridPageComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ MaterialModule, RouterTestingModule, ReactiveFormsModule, HttpModule, BrowserAnimationsModule, HttpClientModule ],
declarations: [
GridPageComponent, NotificationsComponent, SidenavComponent,
HeaderComponent, PlotToolbarComponent, GridFiltersComponent,
LoaderComponent, CalendarGridPlotComponent, DropDownMenuComponent
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(GridPageComponent);
component = fixture.componentInstance;
// fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
// ===========================================================================
// Variable specs
// ===========================================================================
fit('should spinnerLoader variable to be true', () => {
expect(component.spinnerLoader).toBeTruthy();
});
fit('should barLoader variable to be true', () => {
expect(component.barLoader).toBeTruthy();
});
fit('should isDarkTheme variable to be true', () => {
expect(component.isDarkTheme).toBeFalsy();
});
fit('should sideNavState variable to be close', () => {
expect(component.sideNavState).toBe('close');
});
fit('should form variable to be undefined', () => {
expect(component.form).toBeUndefined();
});
});
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from './guards/auth.guard';
// PAGES
// STATUS PAGES
import { LoginComponent } from './pages/login/login.component';
import { Page404Component } from './pages/status/page404/page404.component';
import { DashboardComponent } from './pages/dashboard/dashboard.component';
import { InventoryComponent } from './pages/inventory/inventory.component';
import { LocationAnalyticsComponent } from './pages/analytics/location-analytics/location-analytics.component';
import { TimelinePageComponent } from './pages/analytics/timeline-page/timeline-page.component';
import { GridPageComponent } from './pages/analytics/grid-page/grid-page.component';
import { StoreSelectionPageComponent } from './pages/store-selection-page/store-selection-page.component';
import { SettingsComponent } from './pages/settings/settings.component';
export const routes: Routes = [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full', canActivate: [AuthGuard] },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: 'store-selection', component: StoreSelectionPageComponent, canActivate: [AuthGuard] },
{ path: 'inventory', component: InventoryComponent, canActivate: [AuthGuard] },
{ path: 'analytics/timeline', component: TimelinePageComponent, canActivate: [AuthGuard] },
{ path: 'analytics/grid', component: GridPageComponent, canActivate: [AuthGuard] },
{ path: 'analytics/location', component: LocationAnalyticsComponent, canActivate: [AuthGuard] },
{ path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] },
{ path: '**', component: Page404Component }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
You are trying to pass value in url like 您正在尝试在url中传递值,例如
http://localhost:9876/?id=34428211
and your default ulr is 而您的默认ulr是
{ path: '', redirectTo: 'dashboard', pathMatch: 'full', canActivate: [AuthGuard] },
you applied the canActivate: [AuthGuard]
ANgular Auth gard for it so request goes find the nest route. 您
canActivate: [AuthGuard]
应用了canActivate: [AuthGuard]
Angular Auth gard,因此请求可以找到嵌套路由。 and there is no any route like your request ?id=34428211 并且没有像您的请求一样的路线?id = 34428211
please try this code 请尝试此代码
Your route like this 你这样的路线
{ path: 'login:id', component: LoginComponent },
url like this 像这样的网址
http://localhost:9876/login/34428211
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.