2017-09-27 60 views
2

我申請測試我的角度應用:角4 - 測試組件出現另一個組件

ng test 

這是我的HTML文件(網頁未found.component.html):

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

當我運行ng測試時,組件給我錯誤。

此文件規範(頁面未found.component.spec.ts)

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

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

這是分量代碼(PAGE-未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

但我不明白這個做法,因爲菜單是一個組件,但它沒有任何路線在我的角度路線文件 – Eladerezador

+0

它工作正常,我創建了一個文件ts模擬(activated-route.ts),但我不明白爲什麼?我需要這個文件來測試頁面組件(MenuComponent)裏面的組件PageNotFoundComponent的html,我將搜索更多的文檔 – Eladerezador