【发布时间】:2015-12-04 04:26:57
【问题描述】:
我不想使用 karma-ng-html2js-preprocessor 或 $httpBackend。我有一个动态创建的 templateCache 模块。
app.js
angular.module('myApp', ['ngRoute', 'ui.bootstrap', 'ui.router', 'appTemplates']);
模板.js
(function(){
'use strict';
angular.module('appTemplates', []).run(['$templateCache',
function($templateCache) {
$templateCache.put('js/Templates/datetimepicker.html', '<div>My html here</div>');
}
]);
})();
还有一个指令
datetimepicker.js
angular.module('myApp').directive('datetimepicker',
function () {
return {
restrict: 'A',
replace: false,
templateUrl: 'js/Templates/datetimepicker.html'
};
}
);
问题是,我的测试在编译指令时似乎不想使用templateCache。
test.js
(function () {
"use strict";
describe("Datetimepicker directive tests", function () {
var scope, templateCache, element;
// load the directive's module
beforeEach(module('myApp'));
beforeEach(inject(function ($rootScope, $templateCache) {
scope = $rootScope;
templateCache = $templateCache;
}));
it('should exist', inject(function ($compile) {
//this console.log prints out the correct HTML from cache
//console.log(templateCache.get('js/Templates/datetimepicker.html'));
element = angular.element('<div data-datetimepicker></div>');
element = $compile(element)(scope);
// this logs the element
console.log(element);
//this $digest call throws the error
scope.$digest();
console.log(element);
expect(element.html()).toContain('div');
}));
});
})();
我明白了:
Error: Unexpected request: GET template/datepicker/datepicker.html
当我运行测试时,我的控制台中的 $httpBackend 预计不会有更多请求。
任何帮助表示赞赏。谢谢
【问题讨论】:
-
线索在错误中。 datetimepicker 指令包含一个名为 datepicker 的 Angular-UI 指令。正是这导致了错误。我猜我的指令不可单元测试。
标签: javascript angularjs jasmine angular-templatecache