我想用karma和jasmine测试一个角度指令。该指令使用名为complete.html的外部模板。
我尝试使用以下代码获取html模板:
describe('directive', function () {
beforeEach(module('sampleapp'));
it('test',
function(){
inject(function ($rootScope, $compile) {
var $scope = $rootScope.$new();
var element = $compile(angular.element('<auto-complete></auto-complete>'))($scope);
$scope.$digest();
var textarea = element.find('textarea');
expect(textarea.length).toBe(1);
});
}
); });
然后我收到此错误:
Error: Unexpected request: GET test/complete.html
我搜索了我的问题的解决方案,发现这篇文章: Unit Testing AngularJS directive with templateUrl
我按照业力解决方案的指示。 我的karma.conf.js如下所示:
plugins : [
'karma-ng-html2js-preprocessor',
'karma-chrome-launcher',
'karma-jasmine'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'../complete.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: "complete.html"
},
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'../src/main/web/bower_components/jquery/dist/jquery.min.js' ,
'../src/main/web/bower_components/angular/angular.min.js' ,
'../src/main/web/bower_components/angular-route/angular-route.js',
'../src/main/web/bower_components/angular-mocks/angular-mocks.js',
'../complete.html',
'../src/main/web/scripts/application.js',
'../src/main/web/scripts/main.js',
'../src/main/web/scripts/service.js',
'../src/main/web/scripts/plz.json',
'tests.js'
],
(我明显地裁掉了不必要的部分)
然后按照指示我尝试在我的测试文件中加载生成的模块complete.html:
describe('directive', function () {
beforeEach(module('sampleapp'));
beforeEach(module('complete.html'));
it('test',
function(){
inject(function ($rootScope, $compile) {
var $scope = $rootScope.$new();
var element = $compile(angular.element('<auto-complete></auto-complete>'))($scope);
$scope.$digest();
var textarea = element.find('textarea');
expect(textarea.length).toBe(1);
});
}
);});
但我仍然收到错误Error: Unexpected request: GET test/complete.html
我觉得我很想做一个重要的部分,但无法找到适合我所处情况的完整解决方案。如何正确使用生成的模块以便我不会收到错误?
我非常感谢你的帮助。