错误:路由的配置无效'':遇到未定义的路由。

时间:2017-09-19 17:03:30

标签: angular unit-testing angular-ui-router karma-jasmine

尝试在Angular 4中测试单元并获得以下错误:

Error: 
          Invalid configuration of route '': Encountered undefined route.
          The reason might be an extra comma.
          Example:
          const routes: Routes = [
            { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
            { path: 'dashboard',  component: DashboardComponent },, << two commas
            { path: 'detail/:id', component: HeroDetailComponent }
          ];

然而,如果在RouterTestingModule.withRoutes()中手动添加我的路由路径,它们看起来很完美,所以我得出结论,我从app-routing.module.ts导入的路由正在搞乱某个地方? / p>

应用-routing.module.ts

import {Routes, RouterModule} from '@angular/router';
import {ModuleWithProviders} from '@angular/core';
import {ScreenComponent} from './screen/screen.component';

const router: Routes = [
  {path: '', redirectTo: 'https://www.examplehere.com', pathMatch: 'full'},
  {path: 'home/:rmId/:leadId', component: ScreenComponent},
  {path: '**', redirectTo: 'https://www.examplehere.com'}
];

export const routes: ModuleWithProviders = RouterModule.forRoot(router)

规范文件

import { ScreenComponent } from './screen/screen.component';
import { async, getTestBed, inject, TestBed } from '@angular/core/testing';
import { DropdownModule } from 'primeng/primeng';
import { FormsModule } from '@angular/forms';
import { ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { Http } from '@angular/http';
import { AppGlobalService } from './shared/app.global.service';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
import { routes } from './app-routing.module';

describe('component: ScreenComponent', () => {
      let router, location, injector;

      beforeEach(() => {
        TestBed.configureTestingModule({
          imports: [
            DropdownModule,
            ReactiveFormsModule,
            FormsModule,
            RouterTestingModule.withRoutes([
              // {path: 'home/:rmId/:leadId', component: ScreenComponent}
              router
            ])],
          declarations: [ ScreenComponent ],
          providers: [ {provide: AppGlobalService}, {provide: Http} ]
        });

        beforeEach(inject([Router, Location], (_router: Router, _location: Location) => {
          location = _location;
          router = _router;
        }));

        injector = getTestBed();
        location = injector.get(Location);
        router = injector.get(Router);

      });

      it('should go home', async(() => {
        const fixture = TestBed.createComponent(ScreenComponent);
        router.navigate(['home/:rmId/:leadId']).then(() => {
          expect(location.path()).toBe('/home/:rmId/:leadId');
        });
      }));

    });

0 个答案:

没有答案