2

ng-html2js プラグインについては多くの質問がありますが、残念ながらすべての回答が y の問題を解決するのに役立ちませんでした。

公式のインストール ガイドと例https://github.com/vojtajina/ng-directive-testing/blob/master/test/tabsSpec.jsに従いました。

私の目標は、templateUrl プロパティを持つディレクティブをテストすることです。これは私の構成です

カルマ.conf.js

files: [
  ... // lots of studd here

  'app/views/**/*.html' // templates are all here
],

[...] // other karma configs here

preprocessors: {
  'app/views/**/*.html': ['ng-html2js']
},

テストしたいディレクティブは本当に基本的なものです

'use strict';

angular.module('myAwesomeApp').directive('rzMenu', function () {
    return {
      templateUrl: 'views/directives/rzmenu.html',
      restrict: 'E'
    };   
});

このための単体テスト ファイル

'use strict';

describe('Directive: rzmenu', function () {

  // load the directive's module
  beforeEach(module('myAwesomeApp'));
  beforeEach(module('app/views/directives/rzmenu.html'));

  var element,
    scope;

  beforeEach(inject(function ($rootScope, $compile) {
    element = angular.element('<rzmenu></rzmenu>');
    scope = $rootScope.$new();
    element = $compile(element)(scope);
    scope.$digest();
  }));

  it('should have content', inject(function () {
    //scope.$digest();
    var content = element.text();
    expect(content).toBe('');
  }));
});

「コンテンツが必要」テストを見てください。テンプレートと同じコンテンツであってはなりませんか?

なぜ私は空の文字列を取得するのですか?

前もって感謝します

4

1 に答える 1

4

気にしないでください、私が解決した2つのエラーがありました:

1) cacheIds は templateUrl プロパティと一致する必要があるため、

stripPrefix: 'app/'

テスト内で app/ プレフィックスなしでモジュールを呼び出すだけでなく、構成で必要でした。

beforeEach(module('views/directives/rzmenu.html'));

2) ディレクティブ名が camelCase であるため、インスタンス呼び出しが間違っていました。ここでは正しいものの下にあります。

element = angular.element('<rz-menu></rz-menu>');

:)

于 2014-09-08T15:06:21.293 に答える