我正在尝试测试使用 templateURL 的 angularJS 指令。在我的一生中,我无法让编译器实际加载 templateURL,即使它已被放入 templateCache。我意识到 karma 会预处理所有模板内容并为每个预加载 templateCache 的模块创建模块,但我希望这将是等效的。
这是一个http://jsfiddle.net/devshorts/cxN22/2/,它演示了发生了什么。
angular.module("app", [])
.directive("test", function(){
return {
templateUrl:"some.html",
replace:true
}
});
//--- SPECS -------------------------
describe("template url test", function() {
var element, scope, manualCompiledElement;
beforeEach(module('app'));
beforeEach(inject(function($rootScope, $controller, $compile, $templateCache){
$templateCache.put("some.html", "<div>hello</div>");
scope = $rootScope.$new();
element = $compile(angular.element('<test></test>'))(scope);
manualCompiledElement = $compile(angular.element($templateCache.get('some.html')))(scope);
scope.$digest();
}));
it("has hello", function() {
expect(element.text()).toContain('hello');
});
it("template cache has contents", function(){
expect(manualCompiledElement.text()).toContain('hello');
});
});
我错过了什么?