2

Karma+Jasmine で Angular ディレクティブをテストしようとしています。したがって、私のディレクティブにあるようtemplateUrlに、テンプレートをkarma-ng-html2js-preprocessorでキャッシュする必要があります。私のkarma.conf.jsファイル:

...
files: [
  ...
  'static/views/**/*.html', // to match my directive template
  ...
],

preprocessors: {
  '/static/views/**/*.html': ['ng-html2js]  // to catch the filename as specified in templateUrl 
},

ngHtml2JsPreprocessor: {
  moduleName: 'templates'
}
...

そして私のジャスミン仕様:

describe('Directive unittesting', function() {
  beforeEach(module('myModule'));
  beforeEach(module('templates'));

  var $compile, $scope;

  beforeEach(inject(function(_$compile_, _$rootScope_) {
    $compile = _$compile_;
    $scope = _$rootScope_.$new();
  }));

  it('Replaces the element with the appropriate content', function() {
    var element = $compile("<my-directive></my-directive>")($scope);
    expect(element.html()).toContain("text to contain");
  });

});

しかし、何らかの理由で私のtemplatesモジュールを注入できないようです: TypeError: undefined is not a function (evaluating '$compile(.... それらを正しく連携させるにはどうすればよいですか?

4

1 に答える 1

2

したがって、問題は構成を変更することで解決されました

ngHtml2JsPreprocessor: {
  prependPrefix: '/',
  moduleName: 'templates'
},

プリプロセッサには実際には先頭のスラッシュがありません

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

したがって、実際にはディレクティブでは、テンプレートへのパスは正確に this のようになるはず/statics/views/directives/my-directive.htmlです。$scope.$digest();そしてもう1つ、すべてのコンテキストでレンダリングされるディレクティブを含めることを忘れないでください。

于 2016-04-29T17:05:07.287 に答える