2014-10-29 19 views
0

我正在嘗試使用業力預處理器來獲取單元測試中加載的模板。使用karma運行angularjs單元測試時,無法實例化模塊錯誤html2js預處理器

我的angularjs單元測試指令在運行測試時拋出'無法實例化模塊錯誤'。我在karma.conf.js中使用karma html2js預處理器來獲取指令單元測試中的模板。

karma.conf.js

config.set({ 
     preprocessors: { 
      'Components/navBar/Templates/navBar.htm': ['ng-html2js'] 
     }, 

    files: [ 
    'Scripts/angular-mocks.js', 
    ... 
    ... 
    'Client/Components/navBar/Templates/navBar.htm' 
    ], 
    ngHtml2JsPreprocessor: { 
     moduleName: 'templates' 
    } 

navBar_testcase.js

describe("navbartesting", 
function() { 
    var template; 

beforeEach(module('templates')); 

beforeEach(function() { 
    module("navBar"); 
    inject(function ($rootScope, $compile, $httpBackend, $injector, $localStorage, $state, $templateCache, userSession, appConfig) { 

     http = $httpBackend; 
     $httpBackend = $injector.get('$httpBackend'); 
     ... 
     .... 
    }); 
}); 
}); 

當運行上述的單元測試的錯誤 '無法實例模塊 '模板'' 被拋出。這可能是什麼問題?我們是否有任何物理路徑被定義爲在karma配置文件中具有「模板」模塊?

回答

0

使用此配置,可以製作模板。缺點是,它會創建只有view.tpl.html而不是test.tpl.html

config.set({ 
     autoWatch: true, 
     basePath: '../', 
     browsers: ['Chrome'], 
     frameworks: ['jasmine', 'requirejs'], 

     reporters: ['progress'], 

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

     files: [ 
      // App-specific Code 
      {pattern: 'src/**/*.js', included: false}, 

      // 3rd party code 
      {pattern: 'lib/**/*.js', included: false}, 

      //test files 
      {pattern: 'test/unit/**/*.js', included: false}, 

      'test/test-main.js', 
      '**/test.tpl.html', 
      '**/view.tpl.html' 
     ], 
     ngHtml2JsPreprocessor: { 
      enableRequireJs: true, 
      moduleName: 'templates', 
      cacheIdFromPath: function (filepath) { 
       console.log('path=' + filepath.replace(/^.*[\\\/]/, '')); 
       return filepath.replace(/^.*[\\\/]/, ''); 
      }, 
     }, 

     exclude: [ 
      'src/main.js' 
     ], 
     preprocessors: { 
      '**/*.html': ['ng-html2js'] 
    } 
+0

我可以解決由咕嚕使用「ngtemplates」任務生成單元測試用例模板的問題任務跑步者。 – 2015-11-30 12:01:13

相關問題