业力测试没有找到缓存模板

tim*_*mmy 2 angularjs angularjs-directive karma-runner karma-jasmine

我正在尝试测试从URL中提取其模板的指令.

我正在使用Karma的html2js预处理器.以下是我的karma.conf.js的相关配置:

    // list of files / patterns to load in the browser
files: [
    'angular/jquery-1.9.1.min.js',
    'angular/angular.js',
    'angular/angular-*.js',
    'angular/ui-bootstrap-0.6.0.min.js',
    'angular/healthpro-ng-commons.js',
    '../../main/webapp/js/app.js',
    '../../main/webapp/js/**/*.js',
    '../../main/webapp/js/templates/**/*.html',
    'unit/**/*.js'
],

preprocessors: {
    //location of templates
    '../../main/webapp/js/templates/**/*.html': 'html2js'
}
Run Code Online (Sandbox Code Playgroud)

这是我的业力测试的一部分:

describe('NoteDirective', function() {

var fixture, scope, $compile, $rootScope, template;

beforeEach(function() {
module('CrossReference');
});

beforeEach(inject(function($templateCache, _$compile_, _$rootScope_) {
  template = $templateCache.get('../../main/webapp/js/templates/Note.html');
  console.log('Template......', template);
  $templateCache.put('/CrossReference-portlet/js/templates/Note.html', template);   

  $compile = _$compile_;
  $rootScope = _$rootScope_;
}));
Run Code Online (Sandbox Code Playgroud)

});

记录模板表明它未定义.有小费吗?

我也试过像这样调整预处理:

 ngHtml2JsPreprocessor: {
    stripPrefix: '../../main/webapp',
    prependPrefix: '/CrossReference-portlet'
}
Run Code Online (Sandbox Code Playgroud)

然后将模板加载为模块:

beforeEach(module('/CrossReference-portlet/js/templates/Note.html'));
Run Code Online (Sandbox Code Playgroud)

没运气.有任何想法吗?

Dar*_*ger 5

对于那些仍在努力解决这个问题的人:
你可以尝试moduleName在karma配置中添加一个你的html2js预处理器配置

// karma config file
...
ngHtml2JsPreprocessor: {
    // the name of the Angular module to create
    moduleName: "myApp.templates"
}
Run Code Online (Sandbox Code Playgroud)

然后在测试中,您需要将此模块与主模块一起加载,因此它会将模板添加到缓存中

// in your test spec
beforeEach(module('myApp'));
beforeEach(module('myApp.templates'));
beforeEach(inject(function($templateCache) {
    $templateCache.get('path/to/your/file.html');
});
Run Code Online (Sandbox Code Playgroud)

这对我有用.