2014-05-02 40 views
5

運行grunt karma時,其中一條指令的測試在嘗試獲取模板時失敗。我使用ng-html2js作爲預處理器。下面是我的一些karma.conf.js錯誤:Karma角度單元測試期間發生意外請求

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

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

ngHtml2JsPreprocessor: { 
    moduleName: 'templates' 
} 

在我的測試中,我有以下幾點:

'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); 
    })); 
}); 

當我運行測試,我得到

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

似乎預處理器沒有正確注入模板的JavaScript版本。

我也使用在beforeEach(module(''));模板的路徑嘗試,但導致讀取錯誤:

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

我該如何解決這個問題?

回答

8

我有一種同樣的問題。確保你有確切的文件匹配。打開Goog​​le Chrome控制檯並檢查文件路徑是否完全相同。

enter image description here

在上面爲例,我不得不在ngHtml2JsPreprocessor.stripPrefix添加一個「/」的字符串,它的工作。 所以我猜與Yeoman,你應該使用

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

因爲我用的是約曼工具腳手架我的項目,我需要一個stripPrefix添加到ngHtml2JsPreprocessor選項在我karma.conf.js文件:

ngHtml2JsPreprocessor: { 
    moduleName: 'templates', 
    stripPrefix: 'app' 
} 
相關問題