2017-11-09 29 views
0

アム私の角度4.0.0アプリの下でユニットテストに取り組んで、私の本当のコンポーネント内のいくつかの方法が経由して、手動ルーティングを呼んでいる:角度単位テスト:エラー:いずれの経路とも一致できません。 URLセグメント:「ホーム/顧問」

method(){ 
.... 
    this.navigateTo('/home/advisor'); 
.... 
} 

navigateToは、カスタムルーティングですメソッドの呼び出しこの:

import ...  // Components and dependencies 

const homeRoutes: Routes = [ 
    { 
    path: 'home', component: HomeComponent, 
    children: [ 
     { 
     path: 'registration', 
     component: RegistrationComponent, 
     children: [ 
      { 
      path: 'synthese', 
      component: SyntheseComponent 
      }, 
      { 
      path: 'queue', 
      component: QueueComponent, 
      children: [ 
       { 
       path: 'queue-modal', 
       component: QueueModalComponent 
       }, 
       { 
       path: 'confirm', 
       component: ConfirmComponent 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     path: 'toolbox', 
     component: ToolboxComponent 
     }, 
     { 
     path: 'appointment', 
     component: AppointmentRegistrationComponent 
     }, 
     { 
     path: 'appointment-validation', 
     component: AppointmentValidationComponent 
     }, 
     { 
     path: 'datepicker', 
     component: DatePickerComponent 
     }, 
     { 
     path: 'validation/:defaultNumber', 
     component: ValidationComponent, 
     children: [ 
        { 
        path: 'synthese', 
        component: SyntheseComponent 
        } 
        ] 
     }, 
     { 
     path: 'modalField', 
     component: ModalFieldComponent 
     }, 
     { 
     path: 'search', 
     component: SearchComponent 
     }, 
     { 
     path: 'advanced-search', 
     component: AdvancedSearchComponent 
     }, 
     { 
     path: 'tools', 
     component: ToolsComponent 
     }, 
     { 
     path: 'advisor', 
     component: AdvisorComponent 
     }, 
     { 
     path: 'pilote', 
     component: PilotComponent 
     }, 
     { 
      path: 'blank', 
      component: BlankComponent 
     }, 
     { 
     path: 'view-360/:id', 
     component: View360Component, 
     children: [ 
      { 
      path: 'client', 
      component: ClientComponent 
      }, 
      { 
      path: 'tools', 
      component: ToolsAdvisorComponent 
      }, 
      { 
      path: 'valid-close', 
      component: ValidCloseComponent 
      }, 
      { 
      path: 'application', 
      component: ApplicationView360Component 
      } 
     ], 
     canActivate: [AuthGuardAdviser] 
     }, 
     { 
     path: 'view-360', 
     component: View360Component, 
     children: [ 
      { 
      path: 'client', 
      component: ClientComponent 
      } 
     ], 
     canActivate: [AuthGuardAdviser] 
     }, 
     { 
     path: 'contract', 
     component: ContractComponent 
     }, 
     { 
     path: 'queue-again', 
     component: QueueAgainComponent 
     }, 
     { 
     path: 'stock', 
     component: StockComponent, 
     children: [ 
      { 
      path: 'mobile', 
      component: MobileComponent 
      }, 
      { 
      path: 'stock-level', 
      component: StockLevelComponent 
      } 
     ] 
     }, 
     { 
     path: 'usefull-number', 
     component: UsefullNumberComponent 
     }, 
     { 
     path: 'admin', 
     loadChildren: 'app/home/admin/admin.module#AdminModule', 
     //   component: AdminComponent, 
     canActivate: [AuthGuardAdmin] 
     }, 
     { 
     path: 'rb', 
     loadChildren: 'app/home/rb/rb.module#RbModule', 
     //  component: RbComponent 
     //  canActivate: [AuthGuardAdmin] 
     }, 
     { 
     path: 'tools-advisor', 
     component: ToolsAdvisorComponent 
     }, 
     { 
     path: 'catalog/:haveClient', 
     component: CatalogComponent 
     }, 
     { 
     path: 'application', 
     component: ApplicationComponent 
     }, 
    ] 
    }, 

    ]; 

@NgModule({ 
    imports: [ 
    CommonModule, 
    RouterModule.forChild(homeRoutes) 
    ], 
    exports: [ 
    RouterModule 
    ], 
    declarations: [] 
}) 
export class HomeRoutingModule { } 

S:私はこのルーティングファイルを持っている

public navigateTo(url: string) { 
    this.oldUrl = this.router.url; 
    this.router.navigate([url], { skipLocationChange: true }); 
    } 

奇妙なことに、私のアプリケーションでさえもうまくいっていますが、テストではこのエラーがスローされます:

Failed: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'home/advisor' Error: Cannot match any routes. URL Segment: 'home/advisor'

私はいくつかの設定が不足しているようです。

私のコメントに続いて

+0

ようになります期待していますが、適切に使用すると、アプリのルートを提供するモジュールをインポートしていますか? –

+0

はい、機能的にうまくいきますが、何の問題も見つけられませんでした。ユニットテストだけが失敗しています@Ante JablanAdamović – firasKoubaa

+0

'TestBed'の中でモジュールをインポートし、' router'デバッグモードでブレークポイントを作成し、登録されているルートがあるかどうかを確認します。 –

答えて

1

:あなたのルーターをテストユニットにしたい場合は

は、あなたがテストモジュールではなく、実際のものを使用する必要があります。

その後
import { RouterTestingModule } from '@angular/router/testing'; 

でスタート、あなたのテストベッド

imports: [RouterTestingModule] 

に今、あなたはスパイを作るためにあなたのコンポーネント

EDIT

をテストユニットにできるはずですあなたのルーティングで、あなたは何をしなければなりませんoは

spyOn(component.router, 'navigate').and.returnValue(true); 

であり、あなたが

expect(component.router.navigate).toHaveBeenCalledWith('/home/advisor'); 
+0

私はすでに彼に言及しましたが、彼は理解していません.... –

+0

@trichetriche私は既にRouterTestingModuleをインポートしました。私はそれを行う特定の方法があると思った:RouterTestingModule.withRoutes(appRoutes)問題は、それを追加した後でさえも残っています。RouterTestingModule(理解します;)) – firasKoubaa

+0

@AnteJablanAdamović:RouterTestingModuleでも動作しません。 – firasKoubaa

関連する問題