1

我有一个带有模板网址的角度指令。我正在尝试用业力测试指令。因此,使用 ng-html2js-preprocessor 获取指令的 html。

我的项目文件夹结构是

var
   www
      myproject
           assets
               templetes
                    directive.html
               js
                    directive.js
           karma
               karma.config.js
               directive.test.js

这里 myproject 是我的项目文件夹。所以 var/www 在其他一些环境中可能会有所不同。

在directive.js中我有:

  restrict: 'E',
  scope: true,
  replace: false,
  templateUrl: '/templates/directive.html',

现在在我写的 karma.config.js 文件中

 preprocessors: {
  '../assets/templates/**/*.html': ['ng-html2js']
 },
 files: [
 ..., (Some library files like angular, jquery etc...)
 '../assets/js/directive.js',
  './*.test.js',
  '../assets/templates/**/*.html'
 ]

在日志中的终端中运行配置文件后,我看到:

DEBUG [preprocessor.html2js]: Processing "/var/www/myproject/assets/templates/directive.html".

DEBUG [watcher]: Resolved files:
/var/www/myproject/assets/templates/directive.html.js

在我写的测试文件中

beforeEach(module('/var/www/myproject/assets/templates/directive.html'));

beforeEach(inject(function($rootScope, $compile, defaultJSON, $templateCache) {
var template = $templateCache.get('/var/www/myproject/assets/templates/directive.html');
$templateCache.put('/templates/directive.html', template);
 }));

这段代码工作正常,但我的问题是路径 /var/www 在每个环境中都不相同,可能是其他用户将项目保留在其他路径中,然后它将无法工作。所以我需要添加一些相对路径。谁能告诉我如何在这里添加相对路径?

我试过了

ngHtml2JsPreprocessor: {
  stripPrefix: '*/myproject/assets'
},

在 karma.config.js 文件和测试文件中:

 beforeEach(module('/templates/directive.html'));
 var template = $templateCache.get('/templates/directive.html');

但它没有用,显示错误无法实例化模块'/templates/directive.html'

我也试过

ngHtml2JsPreprocessor: {
  moduleName: "my.templates"
},

在 karma.config.js 文件和测试文件中:

 beforeEach(module('my.templates'));
 var template = $templateCache.get('/templates/directive.html');

但它也失败了。请帮助我如何解决此问题并添加相关网址。

4

0 回答 0