Karma测试失败但调试显示通过

时间:2014-05-01 20:54:48

标签: angularjs unit-testing angularjs-directive jasmine karma-runner

我遇到了一个问题,我在Karma中运行我的茉莉花单元测试,并且我的一个单元测试依赖于外部模板(AngularJS指令)没有通过。当我在浏览器中进入DEBUG模式并且我查看控制台时,它显示此单元测试正在通过,当我在浏览器调试器中单步执行单元测试时,我可以确认这一点。

我还有其他单元测试也依赖于按预期传入Karma的外部模板,但是这个特定的测试在Karma中失败,但是在浏览器控制台中传递。

有没有人有任何想法可能导致这种行为?

编辑:

这是Karma所展示的:

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

我的项目处理模板的方式与AngularJS通常处理它们的方式略有不同。我们创建了一个templateService,它根据指令的templateUrl检索正确的模板,如下所示

templateUrl: "navigation-template"

所以我设置了Karma来拉模板并将它们存储在templateCache中。然后我使用关键的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

如果我在浏览器中调试(当Karma加载浏览器时DEBUG按钮),我可以确认模板在那里并且在浏览器控制台中(不是运行Karma的命令行,而是浏览器中的F12开发人员工具) )它注销测试是否成功,如此

LOG: SUCCESS ButtonsDirectives NavigationDirective Should render a loading message 

以下是我所知道的:

失败的单元测试与传递的单元测试之间没有显着差异。它与单元测试运行的顺序无关(这个顺序恰好先运行)。

1 个答案:

答案 0 :(得分:3)

我已经弄明白了。事实证明,失败单元测试的模板包含以下内容:

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

虽然单元测试本身是相似的,但失败测试是唯一具有此特定标记的模板。

我使用IE 9通过Karma运行我的单元测试,javascript:void(0)在IE 9中出现了问题(我认为它是浏览器,不确定这是否会导致其他浏览器出现问题我目前的设置无法确认。从锚标记中删除javascript:void(0)可以解决我的问题。

编辑:

确认,这是IE 9的一个错误。它在Chrome中运行良好。

相关问题