5

Sto provando a testare la direttiva in angolare usando Jasmine. Ho installato karma-ng-html2js-preprocessore utilizzando NPM. Poi ho istallato jQuery utilizza Bower, ma sto ottenendo questo erroreModule 'templates' non è disponibile! in js angolare?

Connected on socket ndfTI8XJInIU5YJCAAAA with id 49983199 
Chrome 47.0.2526 (Mac OS X 10.10.2) http controller test it should be one FAILED 
     Error: [$injector:modulerr] Failed to instantiate module templates due to: 
     Error: [$injector:nomod] Module 'templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 
     http://errors.angularjs.org/1.4.8/$injector/nomod?p0=templates 
      at /Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:68:12 
      at /Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:2005:17 
      at ensure (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:1929:38) 
      at module (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:2003:14) 
      at /Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/a 

ho fatto direttiva come questa

angular.module('app',[]).controller('first',function($scope,data){ 
    $scope.name='test'; 
    $scope.message='application'; 

    data.getData().then(function(data){ 
      console.log(data); 
    }) 

}).factory('data',function($http){ 
    return{ 
     getData:getData 
    } 

    function getData(){ 
      return $http.get('data.json').success(successCall).error(errorcallback) 
    } 

    function successCall(data){ 
      return data 
    } 
    function errorcallback(data){ 
     return data 
    } 
}).directive('helloWorld',function(){ 
    return { 
     restrict:'E', 
     scope:{ 
      data:'=', 
      message:'@' 
     } , 
     templateUrl:'app/partial/home.html', 
     link:function(s,e,a){ 

     } 
    } 
}) 

home.html 

<div class="dir"> 

    <h1>{{data}}</h1> 
    <h1>{{message}}</h1> 
</div> 

test.js

describe('http controller test', function() { 

    var $rootScope, 
     $scope, 
     $compile, 
     $body=$('body'), 
     el, 
     $el, 
     controller, 
     html='<hello-world data="name" message="{{message}}"></hello-world>'; 

    beforeEach(function(){ 
     module('templates','app') ; 

     inject(function($injector){ 
      $rootScope = $injector.get('$rootScope') ; 
      $compile = $injector.get('$compile') ; 

      $scope=$rootScope.$new(); 
      el=$compile(angular.element(html))($scope) 
      controller =$injector.get('$controller')('first',{$scope:$scope}) 
     }) 
     $body.append(el); 
     $rootScope.$digest(); 

     $el=$('.dir'); 

    }) 

    afterEach(function(){ 
     $body.empty(); 
    }) 

    it('it should be one',function(){ 
     expect($el.length).isEqual(1) 
    }) 

    describe('Init value',function(){ 
     it('check name value',function(){ 
      expect($scope.name).toEqual('test'); 
     }) 

    }) 

    it('it should be true',function(){ 
     expect(true).toBeTruthy(); 

    }) 
}) 

e c'è il karma-config file

// Karma configuration 
// Generated on Fri Dec 18 2015 19:53:32 GMT+0530 (IST) 

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 

     'bower_components/angular/angular.js' , 
     'bower_components/jquery/dist/jquery.js' , 

     'bower_components/angular-mocks/angular-mocks.js' , 
     'bower_components/angular-resource/angular-resource.js' , 
      'app/**/.html', 
     'app/*.js', 
     'test/**.js' 
    ], 


    // list of files to exclude 
    exclude: [ 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
     'app/**/.html':['ng-html2js'] 
    }, 

     ngHtml2JsPreprocessor:{ 
      moduleName:'templates' 

    }, 

    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 

    // Concurrency level 
    // how many browser should be started simultanous 
    concurrency: Infinity 
    }) 
} 

ho già dichiarato il modulo modello come questo aggiornamento

ngHtml2JsPreprocessor:{ 
       moduleName:'templates' 

     }, 

Connected on socket i5kkzULDwF1_ptvcAAAA with id 55433592 
Chrome 47.0.2526 (Mac OS X 10.10.2) http controller test it should be one FAILED 
     Error: [$injector:modulerr] Failed to instantiate module templates due to: 
     Error: [$injector:nomod] Module 'templates' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 
     http://errors.angularjs.org/1.4.8/$injector/nomod?p0=templates 
      at /Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:68:12 
      at /Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:2005:17 
      at ensure (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:1929:38) 
      at module (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:2003:14) 
      at /Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:4435:22 
      at forEach (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:340:20) 
      at loadModules (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:4419:5) 
      at Object.createInjector [as injector] (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular/angular.js:4344:11) 
      at Object.workFn (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular-mocks/angular-mocks.js:2428:52) 
      at window.inject.angular.mock.inject (/Users/naveenkumar/Documents/ionic_work/tes/bower_components/angular-mocks/angular-mocks.js:2411:37) 
     http://errors.angularjs.org/1.4.8/$injector/modulerr?p0=templates&p1=Error%3A%20%5B%24injector%3Anomod%5D%20Module%20'templates'%20is%20not%20available!%20You%20either%20misspelled%20the%20module%20name%20or%20forgot%20to%20load%20it.%20If% 
+0

Hai mai trovato una soluzione a questo? Ho lo stesso identico problema. Grazie. – snowleopard

risposta

0

Non ho precedenti esperienze con karma-ng-html2js-preprocessor quindi questo è più simile a una supposizione - potrebbe essere che template il modulo non è stato generato perché non sono stati trovati modelli.

Potrebbe provare a cambiare questa parte della files sezione del tuo karma-config da:

'app/**/.html' 

a:

'app/**/*.html' 

e preprocessors sezione da:

preprocessors: { 
    'app/**/.html':['ng-html2js'] 
} 

a

preprocessors: { 
    'app/**/*.html':['ng-html2js'] 
} 

Aggiornamento:

Se questo non funziona per voi, è possibile impostare il login livello di Karma a

logLevel: config.LOG_DEBUG 

e provare a cercare per le linee che iniziano con DEBUG [preprocessor.html2js] per raccogliere ulteriori informazioni relative al preprocessore html2js.

+0

controlla l'aggiornamento. Ho usato la tua soluzione. Non è possibile caricare il modello di modulo – user944513

Problemi correlati