15

Angular でディレクティブをテストしようとしていますが、対応するテンプレートが機能しません。

ディレクティブは templateUrl を次のようにリストします

templateUrl: 'directives/listview/view.html'

ユニットテストを書くと、

Error: Unexpected request: GET directives/listview/view.html

だから私は $httpBackend を使用して、次のような賢明なもので応答する必要があります

httpBackend.whenGET('directives/listview/view.html').respond("<div>som</div>");

しかし、実際には、実際のファイルを単純に返し、同期的に実行したいので、待機、遅延オブジェクトなどの問題はありません。どうすればよいですか?

4

2 に答える 2

13

私は今https://github.com/karma-runner/karma-ng-html2js-preprocessorを使用しています。使用するすべてのテンプレートを読み取り、それらを Angular テンプレートに変換し、$templateCache に設定することで、アプリがそれらを必要とするときにキャッシュから取得し、サーバーからは要求しません。

私のカルマconfファイルで

files: [
    // templates
    '../**/*.html'
],

preprocessors : {
  // generate js files from html templates
  '../**/*.html': 'ng-html2js'
},

ngHtml2JsPreprocessor: {
    // setting this option will create only a single module that contains templates
    // from all the files, so you can load them all with module('templates')
    moduleName: 'templates'
},

そして、テストでは、次のようにします

// Load templates
angular.mock.module('templates');

そしてそれはうまくいきます!

于 2013-10-12T15:53:46.123 に答える