2017-09-27 8 views
2

アンギュラ4 - コンポーネントが別のコンポーネントを持っているときのテスト、私は角度の私のアプリケーションにテストを適用しています

:これは私のhtmlファイル(ページ-ない-found.component.html)は

ng test 

です

<menu></menu> 
<div> 
    <h4> 
    ERROR 404 - PÁGINA NO ENCONTRADA 
    </h4> 
</div> 

ng testを実行すると、コンポーネントによってエラーが発生します。

このファイル仕様(ページ未found.component.spec.ts)

import { MenuComponent } from '../menu/menu.component'; 

    beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     declarations: [ PageNotFoundComponent, MenuComponent ] 
    }) 
    .compileComponents(); 
    })); 

これは成分符号(ページ-ない-found.component.ts)

import { Component, OnInit } from '@angular/core'; 

@Component({ 
    selector: 'app-page-not-found', 
    templateUrl: './page-not-found.component.html', 
    styleUrls: ['./page-not-found.component.css'] 
}) 
export class PageNotFoundComponent implements OnInit { 

    constructor() { } 

    ngOnInit() { 
    } 

} 
あります

これはエラーです:

Error: No provider for ActivatedRoute! 

問題を教えてください。ありがとう

答えて

2

あなたのテストベッドにActivatedRouteのプロバイダを用意する必要があります。たとえば、

TestBed.configureTestingModule({ 
     declarations: [ PageNotFoundComponent, MenuComponent ], 
providers: [ 
     {provide: ActivatedRoute, useValue: activatedRoute}, // using a mock 
     ] 
    }) 

このメニューコンポーネントは見つからないコンポーネントにありますか?

+0

はい、必要です、それはヘッダーです、私はテストするつもりです、私はあなたに感謝 – Eladerezador

+0

しかし、この練習をよく理解していない、メニューはコンポーネントですが、それは私の角度routesファイル – Eladerezador

+0

それは正常に動作しています、私はファイルts模擬(activated-route.ts)を作成しましたが、私はundestandをなぜですか?私はこのファイルがコンポーネントのPageNotFoundComponentのhtmlにヘッダーコンポーネント(MenuComponent)をテストするために必要です、私はより多くのドキュメントを検索します – Eladerezador

関連する問題