karma.conf.js未被捕获的referenceerror:google未定义

时间:2014-08-10 04:32:01

标签: javascript angularjs unit-testing karma-runner angularjs-module

当我尝试运行业力测试运行器时,我从我的一个文件中收到以下错误,说我的库谷歌未定义???

   Chrome 36.0.1985 (Mac OS X 10.9.4) ERROR
   Uncaught ReferenceError: google is not defined
   at /Users/giowong/rails_project/doctible_pre_treatment/app/assets/javascripts/angular-google-maps.min.js:7

我的karma.conf.js文件

       // Karma configuration
module.exports = function(config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '/Users/giowong/rails_project/doctible_pre_treatment/',

// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],

// list of files / patterns to load in the browser
files: [
  'app/assets/components/angular/angular.js',
  'app/assets/components/angular-mocks/angular-mocks.js',
  'app/assets/components/angular-resource/angular-resource.js',
  'app/assets/components/angular-payments/lib/angular-payments.js',
  'app/assets/components/ng-file-upload/angular-file-upload.js',
  'app/assets/components/ngDialog/js/ngDialog.js',
  'app/assets/components/angular-route/angular-route.js',
  'app/assets/components/angular-loading-bar/src/loading-bar.js',
  'app/assets/javascripts/angular/filters/widget-filters.js',
  'app/assets/components/angular-animate/angular-animate.js',
  'app/assets/javascripts/angular/directives/loader.js',
  'app/assets/javascripts/angular/directives/focus.js',
  'app/assets/javascripts/angular/directives/checkout-form.js',
  'app/assets/javascripts/angular/directives/provider-form.js',
  'app/assets/components/angular-ui-utils/ui-utils.js',
  'app/assets/components/angular-sanitize/angular-sanitize.js',
  'app/assets/components/angular-bootstrap/ui-bootstrap.js',
  'app/assets/components/angular-ui-map/ui-map.js',
  'app/assets/components/underscore/underscore.js',
  'app/assets/components/jquery-1.11.1.min.js',
  'app/assets/javascripts/*.js',
  'app/assets/javascripts/**/**/*.js',
  'spec/javascripts/*.js'
],
// list of files / patterns to exclude
exclude:[],

// web server port
port: 8080,

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || 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: ['Chrome'],


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

我已经尝试谷歌,到目前为止没有运气。我已经尝试制作测试文件并定义谷歌本身。任何帮助表示赞赏

2 个答案:

答案 0 :(得分:16)

您收到此错误的原因是您在应用程序中使用google maps v3 API,但在运行测试时没有任何代码会初始化window.google属性。

解决方案1 ​​

this mock编译为javascript,并在配置文件中引用已编译的* .js。

解决方案2

  1. 创建自己的google-mock.js
  2. 将其添加到 karma.config 文件数组中
  3. 为您在应用中使用的每个Google Maps API调用添加stogle到google-mock.js 如下例所示:

    (function(){
      window.google = window.google || {
        maps: {
          Map: function(){},
    // and so on...
        }
      };
    })();
    
  4. 解决方案3

    1. 打开this链接
    2. 将此页面中的所有文字保存到文件 google-maps-api.js
    3. 将此文件的路径添加到 karma.config中的文件数组

答案 1 :(得分:0)

就我而言,在karma.conf.js的文件部分中,我需要带有“ js”扩展名的Google api(不是.ts!):

 files:[
          'src/app/shared/mocks/google-api.js'
      ],

我已经从网址复制并粘贴了真实的Google API: “ https://maps.googleapis.com/maps/api/js?sensor=false

如果您传递网址,它将无法正常工作。 这不是一个很酷的解决方案,但目前至少可以使用。