運行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...
我該如何解決這個問題?