Angularjs 单元测试指令和获取错误:意外请求:获取脚本
您好,我正在进行单元测试,我收到了“错误:意外请求:获取脚本”。。。错误。我没有使用http_后端或类似的东西,但如果我需要,请让我知道。测试的目标是对日期选择日历及其功能进行单元测试。以下是涉及的代码部分: 指令:Angularjs 单元测试指令和获取错误:意外请求:获取脚本,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,您好,我正在进行单元测试,我收到了“错误:意外请求:获取脚本”。。。错误。我没有使用http_后端或类似的东西,但如果我需要,请让我知道。测试的目标是对日期选择日历及其功能进行单元测试。以下是涉及的代码部分: 指令: (function () { 'use strict'; angular .module('td.Datepicker') .directive('tdDatepickerMonth', [DatepickerMonthDirective]);
(function () {
'use strict';
angular
.module('td.Datepicker')
.directive('tdDatepickerMonth', [DatepickerMonthDirective]);
function DatepickerMonthDirective() {
return {
restrict: 'A',
templateUrl: 'scripts/datepicker-month-view.html',
scope: { month: '=' },
controller: function ($scope) {
var vm = this;
vm.$controller = undefined;
vm.monthList = undefined;
vm.setMonth = function setMonth(month) {
vm.selectedMonth = month;
$scope.$emit('selectedMonth', month);
};
function updateMonthList() {
var i;
var months = [];
for (i = 0; i < 12; i++) {
months.push({
name: moment().month(i).format('MMMM').substr(0, 3),
value: i + 1
});
}
vm.monthList = [];
for (i = 0; i < 3; i++) {
vm.monthList.push(months.slice(i * 4, (i * 4) + 4));
}
}
updateMonthList();
if (!_.isUndefined($scope.month)) {
vm.setMonth($scope.month);
}
},
controllerAs: 'vm'
};
}
})();
module.exports = function (config) {
config.set({
basePath: '../',
// files to include, ordered by dependencies
files: [
'src/libraries/angular/angular.js',
'src/libraries/angular-mocks/angular-mocks.js',
'src/libraries/jquery/dist/jquery.min.js',
'src/libraries/jquery-ui/jquery-ui.min.js',
//'src/libraries/moment/min/moment.min.js',
'src/libraries/angular-moment/angular-moment.js',
'src/libraries/lodash/lodash.min.js',
'src/libraries/angular-route/angular-route.js',
'src/libraries/angular-resource/angular-resource.js',
'src/libraries/angular-sanitize/angular-sanitize.js',
'src/libraries/angular-bootstrap/ui-bootstrap-tpls.min.js',
'src/libraries/angular-translate/angular-translate.js',
'src/libraries/angular-translate-loader-static-files/angular- translate-loader-static-files.js',
'src/libraries/angular-ui-sortable/src/sortable.js',
'src/libraries/ng-teradata/modules/**/*.js',
'src/libraries/ng-teradata/modules/**/*.html',
'src/**/*.html',
'src/scripts/**/*.js',
'src/scripts/*.js',
'test/unit/*.js'
],
// files to exclude
exclude: [],
// karma has its own autoWatch feature but Grunt watch can also do this
autoWatch: false,
// testing framework, be sure to install the correct karma plugin
frameworks: ['jasmine'],
browsers: ['PhantomJS'],
preprocessors: {
'**/*.html': ['ng-html2js'],
'mock_data/**/*': ['ng-json2js']
},
// test coverage
'src/scripts/controllers/*.js': ['jshint', 'coverage'],
'src/scripts/directives/*.js': ['jshint', 'coverage'],
'src/scripts/services/app.js': ['jshint', 'coverage'],
'src/scripts/*.js': ['jshint', 'coverage'],
reporters: ['progress', 'coverage'],
logLevel: config.DEBUG,
// list of karma plugins
plugins: [
'karma-jshint-preprocessor',
'karma-coverage',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor',
'karma-phantomjs-launcher'
],
// plugin settings
ngHtml2JsPreprocessor: {
stripPrefix: 'src/'
},
coverageReporter: {
// type of file to output, use text to output to console
type: 'html',
// directory where coverage results are saved
dir: 'test/test-results/coverage/'
// if type is text or text-summary, you can set the file name
// file: 'coverage.txt'
},
junitReporter: {
outputFile: 'test/test-results/test-results.xml'
},
// enable / disable colors in the output (reporters and logs)
colors: true,
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
})
}
html2js设置中缺少模块名称。。或者,您应该在测试中包括带有文件路径(默认)的模块,如
beforeach(模块('scripts/datepicker month view.html')代码>以便在缓存中加载模板,以便后端不会收到意外的模板请求。我只有一个模块,文件树如下:/src/scripts/datepicker-day-directive.js,在同一目录中,补充模板:/src/scripts/datepicker-day-view.html如何在html2js设置中对此进行标记?感谢在使用路径之后,我能够移动并传递该错误,现在我得到了一个“语法错误:解析错误”,我将不得不进行调查。感谢您的回复和支持。我会支持你的意见。
module.exports = function (config) {
config.set({
basePath: '../',
// files to include, ordered by dependencies
files: [
'src/libraries/angular/angular.js',
'src/libraries/angular-mocks/angular-mocks.js',
'src/libraries/jquery/dist/jquery.min.js',
'src/libraries/jquery-ui/jquery-ui.min.js',
//'src/libraries/moment/min/moment.min.js',
'src/libraries/angular-moment/angular-moment.js',
'src/libraries/lodash/lodash.min.js',
'src/libraries/angular-route/angular-route.js',
'src/libraries/angular-resource/angular-resource.js',
'src/libraries/angular-sanitize/angular-sanitize.js',
'src/libraries/angular-bootstrap/ui-bootstrap-tpls.min.js',
'src/libraries/angular-translate/angular-translate.js',
'src/libraries/angular-translate-loader-static-files/angular- translate-loader-static-files.js',
'src/libraries/angular-ui-sortable/src/sortable.js',
'src/libraries/ng-teradata/modules/**/*.js',
'src/libraries/ng-teradata/modules/**/*.html',
'src/**/*.html',
'src/scripts/**/*.js',
'src/scripts/*.js',
'test/unit/*.js'
],
// files to exclude
exclude: [],
// karma has its own autoWatch feature but Grunt watch can also do this
autoWatch: false,
// testing framework, be sure to install the correct karma plugin
frameworks: ['jasmine'],
browsers: ['PhantomJS'],
preprocessors: {
'**/*.html': ['ng-html2js'],
'mock_data/**/*': ['ng-json2js']
},
// test coverage
'src/scripts/controllers/*.js': ['jshint', 'coverage'],
'src/scripts/directives/*.js': ['jshint', 'coverage'],
'src/scripts/services/app.js': ['jshint', 'coverage'],
'src/scripts/*.js': ['jshint', 'coverage'],
reporters: ['progress', 'coverage'],
logLevel: config.DEBUG,
// list of karma plugins
plugins: [
'karma-jshint-preprocessor',
'karma-coverage',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor',
'karma-phantomjs-launcher'
],
// plugin settings
ngHtml2JsPreprocessor: {
stripPrefix: 'src/'
},
coverageReporter: {
// type of file to output, use text to output to console
type: 'html',
// directory where coverage results are saved
dir: 'test/test-results/coverage/'
// if type is text or text-summary, you can set the file name
// file: 'coverage.txt'
},
junitReporter: {
outputFile: 'test/test-results/test-results.xml'
},
// enable / disable colors in the output (reporters and logs)
colors: true,
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
})
}