【发布时间】:2014-04-04 17:49:31
【问题描述】:
在花了最后一天尝试完成这项工作后,我发现我又回到了我一开始遇到的同样错误:
错误:意外请求:GET test-directive.html
我正在使用 Karma 和 Jasmine 来测试 Angular 中的指令。我在 StackOverflow 上查看了类似的问题,但发现在其他示例中尝试的所有方法都无济于事。
代码结构
测试应用
-src
--凉亭
--lib
--js
--模块
---testDir
----test.js
----test-directive.html
----测试
-----test.spec.js
-测试
--配置
---karma.conf.js
--e2e
业力配置
'use strict';
module.exports = function(config){
config.set({
basePath: '../../',
frameworks: ['jasmine'],
files: [
// Angular
'src/bower/angular/angular.js',
// Mocks
'src/bower/angular-mocks/angular-mocks.js',
// Libraries
'src/lib/**/*.js',
// App
'src/js/*.js',
'src/modules/*/*.js',
// Tests
'src/modules/**/test/*spec.js',
// Templates
'src/modules/**/*.html'
],
autoWatch: false,
singleRun: true,
reporters: ['progress'],
browsers: ['PhantomJS'],
preprocessors: {
'src/modules/**/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
moduleName: 'dir-templates'
},
plugins: [
'karma-jasmine',
'karma-ng-html2js-preprocessor',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-junit-reporter'
]
});
};
test.js
'use strict';
angular.module('modules.test', []).
directive('testDirective', [function() {
return {
restrict: 'E',
templateUrl: 'test-directive.html',
link: function($scope, $elem, $attrs) {
$scope.someFn = function() {
angular.noop();
};
}
};
}]);
test-direct.html
<span>Hello World</span>
test.spec.js
'use strict';
describe('test module', function() {
beforeEach(module('modules.test'));
/* -- DIRECTIVES------------------ */
describe('directives', function() {
var $compile, $scope, elm;
beforeEach(module('dir-templates');
beforeEach(inject(function($compile, $rootScope) {
$scope = $rootScope.$new();
elm = angular.element('<test-directive></test-directive>');
$compile(elm)($scope);
$scope.$digest();
}));
it('should have one span tag', function(){
//Jasmine test here to check for one span tag.
});
});
});
已经缩短了几个文件以解决问题所在。在调用beforeEach(module('dir-templates')) 时,它应该将所有匹配的 .html 文件加载到 $templateCache 并阻止引发错误的 GET 请求。
任何帮助都将不胜感激,因为它真的让我发疯了。如果您有任何其他问题,请发表评论。
【问题讨论】:
标签: angularjs unit-testing angularjs-directive karma-runner