2016-05-09 7 views
0

После нескольких дней исследований и многих примеров я не могу получить директиву с шаблономUrl для запуска в тесте кармы (директива ведет себя так, как я ожидаю, в обычном приложении).Карма директивы templateUrl

Вот что у меня есть:

karma.conf.js:

... 

// list of files/patterns to load in the browser 
files: [ 
    'js/vendor/angular/angular.min.js', 
    'js/vendor/angular/angular-mocks.js', 
    'js/common/module.js', 
    'js/common/*Service.js', 
    'js/common/*Factory.js', 
    'moduleToTest/js/module.js', 
    'moduleToTest/js/controller.js', 
    'moduleToTest/js/directive.js', 
    'moduleToTest/index.html', 
    'test/*tests.js' 
], 

// preprocess matching files before serving them to the browser 
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
preprocessors: { 
    // karma-ng-html2js-preprocessor for templates in directives 
    'moduleToTest/index.html': 'ng-html2js' 
}, 
... 

Карма Тест:

describe('Directive Tests',function(){ 
    var $compile, $rootScope, $scope, element; 

    //beforeEach(module('./moduleToTest/index.html')); // ??? 

    beforeEach(inject(
     ['$compile','$rootScope',function($c,$r){ 
      $compile = $c; 
      $rootScope = $r; 
      $scope = $r.$new(); 
      element = angular.element('<module-to-test-dir></module-to-test-dir>'); 
      $compile(element)($scope); 

      //$r.$digest(); // Load the template ??? 
     }] 
    )); 

    it('should have N inputs',function(){ 
     expect(element.html()).not.toBe([]); //element.html()===[] right now 
    }); 
}); 

moduleToTest/JS/directive.js

angular.module('ModuleToTest').directive('moduleToTestDir',moduleToTestDir); 
function moduleToTestDir(){ 
    return { 
     restrict: 'E', 
     controller: 'moduleToTestCtrl', 
     templateUrl: 'moduleToTest/index.html' 
    }; 
}; 

Любые предложения, вопросы, разъяснения или ответы приветствуются!

ответ

1

ОК, я отвечаю на свой вопрос, но, надеюсь, это поможет следующему человеку, пытающемуся научиться Карме.

karma.conf.js - неправильный порядок файла:

// list of files/patterns to load in the browser 
files: [ 
    'js/vendor/angular/angular.min.js', 
    'js/vendor/angular/angular-mocks.js', 
    'js/common/module.js', 
    'js/common/*Service.js', 
    'js/common/*Factory.js', 
    'moduleToTest/index.html', // --> Needs to come before .js files <-- 
    'moduleToTest/js/module.js', 
    'moduleToTest/js/controller.js', 
    'moduleToTest/js/directive.js', 
    'test/*tests.js' 
], 

Карма Тест:

describe('module testing', function(){ 

    beforeEach(module('ModuleToTest')); 

    ... // Other component tests on module 

    describe('Directive Tests',function(){ 
     var $compile, $rootScope, $scope, element; 

     beforeEach(module('moduleToTest/index.html')); 

     beforeEach(inject(
      ['$compile','$rootScope',function($c,$r){ 
       $compile = $c; 
       $rootScope = $r; 
       $scope = $r.$new(); 
       element = angular.element('<module-to-test-dir></module-to-test-dir>'); 
       $compile(element)($scope); 

       $r.$digest(); // Load the template 
      }] 
     )); 

     it('should not be empty',function(){ 
      expect(element.html()).not.toBe(''); 
     }); 
    }); 

}); 
Смежные вопросы