我正在使用Karma和Mocha构建单元测试。测试我的指令,并使用html2js(它将htmls转换为$ templateCache中的缓存字符串)。 有趣的是,在我的测试中使用$ rootScope。$ new()时,模板html不会进入指令。下面的代码:
it('should show a thumb name', function() {
inject(function($compile, $rootScope,$controller) {
var scope = $rootScope;//.$new() ($new not working. Why?)
var linkFn = $compile('<thumb></thumb>');
var element = linkFn(scope);
scope.$digest(); // <== needed so that $templateCache will bring the html
// (that html2js put in it)
console.log(element.html());// correctly returns thumb's directive templateUrl content
}));
...
然而,如果我使用范围= $ rootScope $新的(),该element.html()将返回一个空字符串
有任何想法吗?
千恩万谢 利奥尔
您需要提供所有的代码。测试中其他地方是否定义了元素? thumb是一个指令吗? – matsko