1

テンプレート URL を含む角度ディレクティブがあります。カルマでディレクティブをテストしようとしています。ng-html2js-preprocessor でディレクティブの html を取得します。

私のプロジェクトフォルダー構造は

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

ここで、myproject は私のプロジェクト フォルダーです。そのため、他の環境では var/www が異なる場合があります。

私はディレクティブ.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');

しかし、それも失敗しました。この問題を修正して相対 URL を追加する方法を教えてください。

4

0 に答える 0