2

次のディレクティブをテストしようとしています。

angular.module('myModule.directives', [])
  .directive('test', function() {
    return {
      restrictions: 'E',
      scope: {},
      transclude: true,
      replace: true,
      template: '<div><ui><li>a</li></ul></div>'
    }
});

私のテストでは:

describe('directives', function () {
    var compile,
    scope,
    test;

    beforeEach(module('myModule.directives'));
    beforeEach(inject(function ($compile, $rootScope) {

    compile = $compile;
    scope = $rootScope;
    test = angular.element('<test></test>');
    compile(test)(scope);
    scope.$digest();

    it('should render the test directive', function () {
        var lis = test.find('li');
        console.log(lis.length); // should output 1
    });

}));  

テストは失敗し、0 が出力されます。コンパイルが実際に何かを行っているかどうかが気になります。テストを次のように置き換えると

  test = angular.element("<test><li></li></test>");. 

次に、「検索」はテスト要素内でliを見つけます。これは、コンパイルがレンダリングされていないことを示しています

  '<div><ui><li>a</li></ul></div>' 

あるべきように。何か案は?ガイダンスとして次のビデオを見ました ( http://www.youtube.com/watch?v=rB5b67Cg6bc )。

4

1 に答える 1