2014-05-02 14 views
5

Quando si esegue grunt karma, un test su una delle direttive non riesce quando tenta di recuperare il modello. Sto usando ng-html2js come preprocessore. Ecco alcuni dei miei karma.conf.js'Errore: richiesta inattesa' durante il test dell'unità angolare Karma

plugins: ['karma-chrome-launcher', 
      'karma-jasmine', 
      'ng-html2js', 
      'karma-ng-html2js-preprocessor'], 

preprocessors: { 
    'app/scripts/directives/**/*.html': 'ng-html2js' 
}, 

ngHtml2JsPreprocessor: { 
    moduleName: 'templates' 
} 

Nella mia prova, ho il seguente:

'use strict'; 

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

    // load the directive's module 
    beforeEach(module('myApp')); 
    beforeEach(module('templates')); 

    var element, 
    scope; 

    beforeEach(inject(function ($rootScope) { 
    scope = $rootScope.$new(); 
    })); 

    it('should not show search area initially', inject(function ($compile) { 
    element = angular.element('<navbar></navbar>'); 
    element = $compile(element)(scope); 
    scope.$digest(); 
    expect(element.find('.myClass').hasClass('myClass')).toBe(true); 
    })); 
}); 

Quando eseguo il test, ottengo

Error: Unexpected request: GET /scripts/directives/myDirective/myDirective.html

Sembra che il preprocessore non stia iniettando correttamente la versione javascript del modello.

ho anche provato ad utilizzare il percorso del modello nella beforeEach(module('')); ma che causa un errore che recita:

Error: [$injector:modulerr] Failed to instantiate module...

Come posso risolvere questo problema?

risposta

8

ho avuto tipo dello stesso problema. Assicurati di avere la corrispondenza esatta del file. Apri la console di Google Chrome e verifica che il percorso del file sia esattamente lo stesso.

enter image description here

Nel exemple superiore, ho dovuto aggiungere un "/" stringa in ngHtml2JsPreprocessor.stripPrefix e ha funzionato. Quindi suppongo che con Yeoman dovresti usare

ngHtml2JsPreprocessor: { 
    moduleName: 'templates', 
    stripPrefix: 'app/' //add a slash 
} 
0

Dal momento che stavo usando lo strumento Yeoman al patibolo il mio progetto, ho bisogno di aggiungere un stripPrefix all'opzione ngHtml2JsPreprocessor nel mio file karma.conf.js:

ngHtml2JsPreprocessor: { 
    moduleName: 'templates', 
    stripPrefix: 'app' 
} 
Problemi correlati