2017-03-20 46 views
1

我已經創建了出口的引導模態的成分「EXPORTAS」:角2的單元測試:沒有指令與

@Component({ 
    selector: 'app-modal', 
    templateUrl: './modal.component.html', 
    styleUrls: ['./modal.component.scss'], 
    exportAs: 'modal' 
}) 

然後父分量i消耗它如下上:

<app-modal #modalHandler="modal"></app-modal> 

的問題是當我寫使用NO_ERROR_SCHEMA父組件一個單元測試,因緣失敗,出現以下錯誤:

There is no directive with "exportAs" set to "modal" 

只有在將模態組件導入TestBed中的父組件後才解析。

看起來像NO_ERROR_SCHEMA不會消除此錯誤。無論如何,我可以避免這個錯誤,而無需將孩子模態導入到我的父母單元測試中?

+0

您是否在多個子模塊中導入ModalModule? – Aravind

+0

不,只有一個。這與問題有什麼關係? –

+0

你需要在你的單元測試中模擬所有依賴模塊的模塊 – Aravind

回答

1

It resolves only after i import the modal component to the the parent component in TestBed.

它應該被導入。因爲在TestBed中,您只是使用父組件創建了一個空模塊。在做這件事時,它也嘗試編譯並執行你的父模板。在那裏,它被蔑視爲

<app-modal #modalHandler="modal"></app-modal> 

所以它會尋找它被定義爲出口爲modal的對象。

在測試用例中,因爲你的模塊是空的,並且不會導入那個app-modal,所以你的父模板不能被初始化。

所以,你需要

import the modal component to the the parent component in TestBed

這是測試牀是如何工作的。您需要導入哪些內容需要您獨立測試您的組件。

注:

Generaly我們使用以下行來創建規範FIEL本身就是一個測試模塊。

TestBed.configureTestingModule({ 
}); 

所以它沒有使用你的父組件存在的模塊。而是在運行時創建一個測試模塊。

+0

謝謝,只是認爲沒有它的方式。由於模態可能有它的依賴關係,我不想事件要測試它在父組件中的功能...... –