2015-04-08 7 views
1

У меня странная проблема, и я застрял.директива компиляции с кармой для углового

Моя задача - проверить список директив в проекте.

Директивы выглядеть следующим образом:

angular.module('lessons', []).directive('uwTextarea', ['usUmpire', 'usConfig', function (usUmpire, usConfig) { 
    return { 
     scope: { 
      settings: "=" 
     }, 
     restrict: 'E', 
     replace: true, 
     transclude : true, 
     templateUrl: 'rki/directive/uw-textarea/uw-textarea.html', 
     controller: function ($scope, $element) { 
      // blah blah 
     } 
    }; 
}]); 

Тестовые выглядеть следующим образом:

describe('uwTextarea', function() { 

    var scope, rscope, elem, compile; 

    beforeEach(module('lessons')); 

    beforeEach(module('directive/uw-textarea/uw-textarea.html')); 

    beforeEach(inject(function($rootScope, $compile) { 
     rscope = $rootScope; 
     compile = $compile; 
    })) 


    it('Directive Compiles', function() { 
     elem = compile('<uw-textarea></uw-textarea>')(rscope); 
     console.log(elem); 
     rscope.$digest(); 

    }); 
}); 

console.log(elem) дает мне эту

LOG: {0: <uw-textarea class="ng-scope"></uw-textarea>, length: 1} 

путь, как я ча n получить мой шаблон от

beforeEach(inject(function($rootScope, $compile, $templateCache) { 
    rscope = $rootScope; 
    compile = $compile; 
    tmpl = $templateCache.get('directive/uw-textarea/uw-textarea.html'); 
})) 

но я думаю, что это не правильно.

karma.conf.js

module.exports = function(config) { 
    config.set({ 
    basePath: '', 
    frameworks: ['jasmine'], 
    files: [ 
     'bower_components/jquery/dist/jquery.min.js', 
     'bower_components/angular/angular.min.js', 
     'bower_components/angular-mocks/angular-mocks.js', 

     'app.js', 

     'service/*.js', 

     'directive/uw-textarea/uw-textarea.js', 
     'directive/uw-textarea/uw-textarea-spec.js', 
     'directive/uw-textarea/uw-textarea.html' 
    ], 
    preprocessors: { 
     'directive/uw-textarea/uw-textarea.html' : 'ng-html2js' 
    }, 
    logLevel: config.LOG_INFO, 
    autoWatch: true, 
    browsers: ['Chrome'], 
    singleRun: false 
    }); 
}; 

Я пытался использовать ng-directive-testing пример для моих тестов, и делать то же мою директиву не компилируется.

Что не так с моим кодом?

ответ

0

Я знаю, что это старый пост, но в последнее время у меня были проблемы с этим. Это пример моих контрольных тестов, которые теперь работают.

describe('Directive', function() { 
    var element, scope, isolatedScope; 

    // Load the directive module and the html file 
    beforeEach(module('directives')); 
    beforeEach(module('path/to/directive/directive.html')); 


    beforeEach(inject(function ($compile, $rootScope) { 

    // Directive 
    var html = '<div somedirective></div>'; 

    // Create the angular element 
    var elem = angular.element(html); 

    // Add stuff to scope if needed 
    scope = $rootScope; 
    scope.settings = 'bar'; 

    //Compile with the scope 
    element = $compile(elem)(scope); 
    scope.$digest(); 

    //Sometimes handy 
    isolatedScope = element.isolateScope(); 

    it(' ... ', function() { /** Test **/ }); 

В дополнении к конфигурации вопросов кармы Я также включил:

// Enabled plugins 
    plugins: [ 
     "ng-html2js", 
     "karma-phantomjs-launcher", 
     "karma-jasmine", 
     "karma-ng-html2js-preprocessor" 
    ] 

Надеется, что это помогает

Смежные вопросы