单元测试指令并获取错误:意外请求:GET脚本

时间:2015-05-27 00:29:39

标签: angularjs unit-testing karma-jasmine

您好我正在进行单元测试,我得到了#34;错误:意外请求:GET脚本" ...错误。我没有使用http_backend或类似的东西,但如果我需要请告诉我。测试的目标是对datepick日历及其功能进行单元测试。以下是涉及的代码部分:

指令:

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

以下是规范: 我从来没有接受基本测试来测试实际的真实性,因为它在&#34;它之前失败了......

describe('Month Picker Directive', function () {
var $rootScope, $compile, $controller, element, $scope, ctrl;
beforeEach(module('td.Datepicker'));

beforeEach(inject(function (_$compile_, _$rootScope_, _$controller_) {
    $rootScope = _$rootScope_;
    $controller = _$controller_;
    $scope = $rootScope.$new();
    $compile = _$compile_;
    element = angular.element("<div td-monthpicker></div>");
    template = $compile(element)($scope);
    $scope.$digest();
    ctrl = template.controller;
}));

it('Should define the controller', inject(function () {
   //ctrl.setMonth = jasmine.createSpy();

    expect(true).toBe(true);
  }));
});

Karma文件:

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

0 个答案:

没有答案