10

カルマ テスト ランナーを実行しようとすると、ファイルの 1 つから次のようなエラーが表示されます。

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

私はすでにグーグルを試しましたが、今のところ運がありません。テストファイルを作成して、Google自体を定義しようとしました。どんな助けでも大歓迎です

4

2 に答える 2

25

アプリケーションで google maps v3 API を使用しているが、テストの実行時に window.google プロパティを初期化するコードがないため、このエラーが発生します。

解決策 1

このモックを JavaScript にコンパイルし、構成ファイルでコンパイルされた *.js を参照します。

解決策 2

  1. 独自の google-mock.js を作成する
  2. karma.configのfiles配列に追加します
  3. 以下の例のように、アプリで使用する Google Maps API 呼び出しごとに google-mock.js に stab を追加します。

    (function(){
      window.google = window.google || {
        maps: {
          Map: function(){},
    // and so on...
        }
      };
    })();
    

解決策 3

  1. このリンクを開く
  2. このページのすべてのテキストをファイルgoogle-maps-api.jsに保存します
  3. このファイルへのパスをkarma.configのファイル配列に追加します
于 2014-08-10T06:03:47.570 に答える