karma-ng-html2js-preprocessor または $httpBackend を使用したくありません。動的に作成した templateCache モジュールがあります。
app.js
angular.module('myApp', ['ngRoute', 'ui.bootstrap', 'ui.router', 'appTemplates']);
templates.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 でこれ以上のリクエストは期待されません。
どんな助けでも感謝します。ありがとう