我不想使用karma-ng-html2js-preprocessor或$ httpBackend。我有一個我動態創建的templateCache模塊。我如何在我的Jasmine測試中使用我的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'
};
}
);
的問題是,我的測試似乎並不想要使用t他在我編譯指令時使用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指令。這是導致錯誤的原因。我想我的指令不是單元測試。 – colincclark