1

我有一個問題,我在Karma運行我的茉莉花單元測試,並且我的一個單元測試依賴於外部模板(AngularJS指令)沒有通過。當我在瀏覽器中進入DEBUG模式,然後查看控制檯時,它顯示此單元測試已通過,我可以在瀏覽器調試器中單步測試時確認這一點。Karma測試失敗,但調試顯示通過

我還有其他的單元測試,也取決於外部模板,正如預期的傳遞在Karma中,但只是這個特定的測試在Karma失敗,但通過瀏覽器控制檯。

有沒有人有任何想法可能會導致此行爲?

編輯:

這裏是噶表示:

TypeError: Unable to get value of the property 'then': object is null or undefined 

我的項目處理模板一點不同於AngularJS通常處理它們。我們有我們創建了一個templateService它檢索基於一個指令,它看起來像這樣

templateUrl: "navigation-template" 

所以我已經建立了噶拉模板,並將其存儲在templateCache的templateUrl正確的模板。然後,我使用關鍵的Karma(文件路徑)提取模板,並使用指令所期望的templateUrl將它們重新插入到templateCache中。以下是我如何做

var template = '<div>' + templateCache.get(templateFilePath) + '</div>'; } 
// template files contain multiple templates so this filters out the one I want 
template = $("[TemplateId='" + directiveTemplateUrl + "']", template).html(); 
templateCache.put(directiveTemplateUrl, template); 

對於所有其他測試,這工作正常,但一個特別是產生上述錯誤。更有趣的是,如果我添加這樣的控制檯日誌

var template = '<div>' + templateCache.get(templateFilePath) + '</div>'; 
if (template.indexOf("undefined") > -1) { throw new Error("Template not in cache."); } 
template = $("[TemplateId='" + directiveTemplateUrl + "']", template).html(); 
if (!template) { throw new Error("Template not found."); } 
templateCache.put(directiveTemplateUrl, template); 

它註銷該模板找不到。

Error: Template not in cache.,Error: Unexpected request: GET navigation-template No more request expected 

如果我調試在瀏覽器中(當噶加載瀏覽器中的DEBUG按鈕),我可以證實的模板是存在的,在瀏覽器控制檯(不到哪噶運行命令行,但F12開發在瀏覽器工具),它記錄的是測試成功這樣

LOG: SUCCESS ButtonsDirectives NavigationDirective Should render a loading message 

這是我知道的:

有失敗的單元測試和傳球者之間沒有顯著差異。它與單元測試運行的順序無關(這只是剛好​​運行)。

+0

「傳入瀏覽器控制檯」是什麼意思,測試失敗時會出現什麼錯誤?當您逐步調試調試器中的代碼時,它會通過這一事實,這聽起來像是一個計時問題。 –

+0

您可以發佈瀏覽器控制檯中的內容嗎? – MarcoL

+0

@SunilD。我認爲*用「DEBUG」表示在Karma中的調試窗口,它不是JS調試器。 – MarcoL

回答

2

我已經想通了。事實證明,失敗的單元測試的模板包含以下內容:

<a href="javascript:void(0)" ... > ... </a> 

雖然單元測試本身是相似的,失敗的測試是在它這個特定標籤的唯一模板。

我正在使用IE 9和javascript:void(0)通過Karma運行我的單元測試,結果在IE 9中有問題(我認爲它是瀏覽器,不確定這是否會導致其他瀏覽器出現問題。建立)。從錨標記中移除javascript:void(0)可解決我的問題。

編輯:

證實,它與IE 9的錯誤它工作正常,在Chrome中。

相關問題