Angular未定义karma-jasmine测试错误

时间:2014-12-10 08:50:23

标签: angularjs unit-testing karma-jasmine

我是使用Karma-jasmine进行测试的新手,但是却出错了。

我的karma.conf.js

files: [
  'test/*Spec.js',
  'app/js/*.js'
],

此错误在命令行中显示

Chrome 39.0.2171 (Windows 7) ERROR
  Uncaught TypeError: Cannot read property 'module' of undefined
  at D:/Test_Samples/WebContent/MyTest/app/js/angular-route.js:24


Firefox 34.0.0 (Windows 7) ERROR
  TypeError: angular is undefined
  at D:/Test_Samples/WebContent/MyTest/app/js/angular-route.js:24


IE 8.0.0 (Windows 7) ERROR
  'undefined' is null or not an object
  at D:/Test_Samples/WebContent/MyTest/app/js/angular-route.js:24

角route.js:24

/* global -ngRouteModule */
var ngRouteModule = angular.module('ngRoute', ['ng']).provider('$route', $RouteProvider),
    $routeMinErr = angular.$$minErr('ngRoute');

2 个答案:

答案 0 :(得分:6)

您必须在测试前导入角度和角度。 这是我使用的配置示例:

files: [
    {pattern: 'src/main/webapp/static/libs/jquery/dist/jquery.js', watch: false},
    {pattern: 'src/main/webapp/static/libs/angular/angular.js', watch: false},
    {pattern: 'src/main/webapp/static/libs/angular-resource/angular-resource.js', watch: false},
    {pattern: 'src/main/webapp/static/libs/angular-mocks/angular-mocks.js', watch: false},
    {pattern: 'src/main/webapp/static/libs/angular-ngkit/js/ngkit.js', watch: false},
    'src/main/webapp/static/templates/angular/*.html',
    'src/main/webapp/static/js/angular/**/*.js',
    'src/test/js/spec/angular/*.js'
 ],

避免观看图书馆(watch: false)是最佳做法,因为这些文件在开发过程中不会发生变化!

定义" basePath"同样重要。属性,因为所有路径都将使用该根解析!

答案 1 :(得分:0)

您需要在任何其他角度文件之前引用angular.js文件。 确保根据安装karma时获得的配置文件检查配置文件。有关详细信息,请参阅此网页。 http://karma-runner.github.io/0.12/config/configuration-file.html