我正在尝试为我的AngularJS应用程序设置业力测试,但我不知何故经常失败。 我已经按照here的所有步骤进行了操作,但我感觉ng-html2js预处理器无法正常工作。我总是得到以下信息:
错误:意外请求:GET js / templates / my-template.template.html
这是我的代码结构
这是我的测试结构
我试图测试的指令是:
angular
.module('myapp')
.directive('myDirective', ['myService', function (myService) {
return {
restrict: 'E',
templateUrl: 'js/templates/my-template.template.html',
scope: {},
link: function (scope) {
}
}
}]);
我的业力骗局看起来像这样:
module.exports = function (config) {
config.set({
basePath: '../../',
frameworks: ['jasmine'],
files: [
//some omitted like angular and so on...
'main/resources/static/**/*.js',
'**/*.html',
'test/js/**/*Spec.js'
],
preprocessors: {
'**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: 'templates'
}
... (more default stuff omitted)
}
和mySpec.js是这样的:
describe('directive', function () {
'use strict';
beforeEach(module('myapp'));
beforeEach(module('templates'));
var elm,
scope;
beforeEach(inject(function ($rootScope, $compile) {
elm = angular.element(
'<my-directive>' +
'</my-directive>');
scope = $rootScope.$new();
$compile(elm)(scope);
scope.$digest();
}));
describe('Does something', function () {
console.log('test');
expect(true).toBe(true);
});
});
答案 0 :(得分:1)
在您的beforeEach函数中添加指令的html模板的模拟模板。使用$templateCache
:
describe('directive', function () {
'use strict';
beforeEach(module('myapp'));
beforeEach(module('templates'));
var elm,
scope;
beforeEach(inject(function ($rootScope, $compile, $templateCache) {
$templateCache.put('js/templates/my-template.template.html','HTML_OF_YOUR_DIRECTIVE');
elm = angular.element(
'<my-directive>' +
'</my-directive>');
scope = $rootScope.$new();
$compile(elm)(scope);
scope.$digest();
}));
describe('Does something', function () {
console.log('test');
expect(true).toBe(true);
});
});
答案 1 :(得分:1)
我想我明白了。解决方案是在karma.conf.js:
ngHtml2JsPreprocessor: {
cacheIdFromPath: function (filepath) {
//The suggested filepath.strip would through an error
var cacheId = filepath.replace('main/resources/static/', '');
return cacheId;
},
moduleName: 'templates'
}