Karma测试未能评估应用程序的角度路线

时间:2014-09-09 17:47:14

标签: angularjs requirejs karma-runner

使用带有RequireJS的AngularJS时设置Karma测试。似乎所有东西都被设置但它标记了'TypeError:'undefined'不是路由文件中的对象(评估'app.config')'错误。

要求配置main.js

 'use strict';

require.config({
  paths: {
    angular: '../../bower_components/angular/angular',
    angularAnimate: '../../bower_components/angular-animate/angular-animate',
    angularFoundationTpls: '../../bower_components/angular-foundation/mm-foundation-tpls',
    angularMocks: '../../bower_components/angular-mocks/angular-mocks',
    angularResource: '../../bower_components/angular-resource/angular-resource',
    angularRoute: '../../bower_components/angular-route/angular-route',
    app: 'app',
    domReady: '../../bower_components/domReady/domReady',
    foundation: '../../bower_components/foundation/js/foundation',
    igniteui: '../libs/igniteui/igniteui-angular',
    igniteuidirectives: 'directives/igniteui-directives',
    infragistics: '../libs/infragistics/infragistics',
    infragisticsdv: '../libs/infragistics/infragistics.dv',
    infragisticslob: '../libs/infragistics/infragistics.lob',
    infragisticscore: '../libs/infragistics/infragistics.core',
    jquery: '../../bower_components/jquery/dist/jquery',
    jqueryui: '../../bower_components/jquery-ui/jquery-ui',
    modernizr: '../../bower_components/modernizr/modernizr',
    text: '../../bower_components/requirejs-text/text',
    tokenInput: '../libs/tokeninput/src/jquery.tokeninput',
    underscore: '../../bower_components/underscore/underscore'
  },

    shim: {
        'angular': {
            deps: ['jquery'],
            exports: 'angular'
        },

        'angularAnimate': ['angular'],

        'angularFoundationTpl': {
            deps: ['foundation', 'angular']
        },

        'angularMocks': {
            deps: ['angular'],
            exports: 'angular.mock'
        },

        'angularResource': ['angular'],

        'angularRoute': ['angular'],

        'app': {
            exports: 'app'
        },

        'foundation': {
            deps: ['jquery', 'modernizr'],
            exports: 'Foundation'
        },

        'igniteui': {
            deps: ['infragisticslob']
        },

        'infragisticsdv': {
            deps: ['infragisticscore']
        },

        'infragisticslob': {
            deps: ['infragisticsdv']
        },

        'infragisticscore': {
            deps: ['angular', 'jqueryui']
        },

        'jquery': {
            exports: '$'
        },

        'modernizr': {
            exports: 'modernizr'
        },

        'tokenInput': {
            deps: ['jquery']
        }

    },

    deps: ['./boot']
});

Requirejs karma config

'use strict';

var tests = [];
for (var file in window.__karma__.files) {
  if (/Spec\.js$/.test(file)) {
    tests.push(file);
  }
}

require.config({
  // Karma serves files from '/base'
  baseUrl: '/base/app/scripts',

  paths: {
    angular: '../../bower_components/angular/angular',
    angularAnimate: '../../bower_components/angular-animate/angular-animate',
    angularFoundationTpls: '../../bower_components/angular-foundation/mm-foundation-tpls',
    angularMocks: '../../bower_components/angular-mocks/angular-mocks',
    angularResource: '../../bower_components/angular-resource/angular-resource',
    angularRoute: '../../bower_components/angular-route/angular-route',
    app: 'app',
    domReady: '../../bower_components/domReady/domReady',
    foundation: '../../bower_components/foundation/js/foundation',
    igniteui: '../libs/igniteui/igniteui-angular',
    igniteuidirectives: 'directives/igniteui-directives',
    infragistics: '../libs/infragistics/infragistics',
    infragisticsdv: '../libs/infragistics/infragistics.dv',
    infragisticslob: '../libs/infragistics/infragistics.lob',
    infragisticscore: '../libs/infragistics/infragistics.core',
    jquery: '../../bower_components/jquery/dist/jquery',
    jqueryui: '../../bower_components/jquery-ui/jquery-ui',
    modernizr: '../../bower_components/modernizr/modernizr',
    text: '../../bower_components/requirejs-text/text',
    tokenInput: '../libs/tokeninput/src/jquery.tokeninput',
    underscore: '../../bower_components/underscore/underscore'
  },

  shim: {

    'angular': {
      deps: ['jquery'],
      exports: 'angular'
    },

    'angularAnimate': ['angular'],

    'angularFoundationTpls': {
      deps: ['foundation', 'angular']
    },

    'angularMocks': {
      deps: ['angular'],
      exports: 'angular.mock'
    },

    'angularResource': ['angular'],

    'angularRoute': ['angular'],

    'app':{
        exports: 'app'
    },

    'foundation': {
      deps: ['jquery', 'modernizr'],
      exports: 'Foundation'
    },

    'igniteui': {
      deps: ['infragisticslob']
    },

    'infragisticsdv': {
      deps: ['infragisticscore']
    },

    'infragisticslob': {
      deps: ['infragisticsdv']
    },

    'infragisticscore': {
      deps: ['angular', 'jqueryui']
    },

    'jquery': {
      exports: '$'
    },

    'modernizr': {
      exports: 'modernizr'
    },

    'tokenInput': {
      deps: ['jquery']
    }

  },

  // ask Require.js to load these files (all our tests)
  deps: tests,

  // start test run, once Require.js is done
  callback: window.__karma__.start
});

最后是业力配置

module.exports = function(config) {
  "use strict";
  config.set({

    // base path, that will be used to resolve files and exclude
    basePath: '../',

    // frameworks to use
    frameworks: ['jasmine', 'requirejs'],

    // list of files / patterns to load in the browser
    files: [
      {pattern: 'bower_components/**/*.js', included: false},
      {pattern: 'app/libs/**/*.js', included: false},
      {pattern: 'app/scripts/**/*.js', included: false},
      {pattern: 'test/**/*Spec.js', included: false},
      {pattern: 'app/views/**/*.html', included: false},
      'test/test-main.js'
    ],


    // list of files to exclude
    exclude: [
      'app/scripts/main.js'
    ],


    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress'],


    // web server port
    port: 9875,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera
    // - Safari (only Mac)
    // - PhantomJS
    // - IE (only Windows)
    browsers: ['PhantomJS'],


    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

路线 -

define([
    'app',
    'controllers/dashboardController',
    'controllers/inventoryController',
    'controllers/permissionsController',
//    'controllers/serviceController',
    'controllers/detailsController',
    'controllers/treeController',
    'controllers/newInventoryController'
    ], function (app) {
    'use strict';
    return app.config(['$routeProvider', function ($routeProvider) {

        $routeProvider.when('/', {
            redirectTo: '/dashboard'
        });

        $routeProvider.when('/dashboard', {
            templateUrl: '../views/dashboard.html',
            controller: 'dashboardController'
        });

        $routeProvider.when('/inventory', {
            templateUrl: '../views/inventory/inventory.html',
            controller: 'inventoryController'
        });

        $routeProvider.when('/inventory/new', {
            templateUrl: '../views/inventory/newInventory.html',
            controller: 'newInventoryController'
        });

        $routeProvider.when('/permissions', {
            templateUrl: '../views/permissions.html',
            controller: 'permissionsController'
        });

//        $routeProvider.when('/services', {
//            templateUrl: '../views/control/services/services.html',
//            controller: 'serviceController'
//        });

//        $routeProvider.when('/services/details', {
//            templateUrl: '../views/control/services/details/details.html',
//            controller: 'detailsController'
//        });

//        $routeProvider.otherwise({redirectTo: '/'});
    }]);

});

我缺少什么?

1 个答案:

答案 0 :(得分:0)

您是否将角度模块引用作为app.js的一部分返回?

define(['angular'], function(angular){
   var app = angular.module('myapp', []);

   //Other code goes here
   return app;

});