Karma和Angular基本设置 - undefined不是函数

时间:2017-06-02 01:04:15

标签: angular unit-testing karma-runner karma-mocha

我正在尝试让Karma设置单元测试我的应用程序。我现在只关注我的Angular前端来解决它(我计划将Karma用于我的Node后端)。

我有一个非常简单的单元测试,它绝对没有任何东西,每次都会通过。我可以通过在命令行上使用mocha执行我的测试来验证这一点。但是,我正在努力让我的测试与Karma一起运行。我得到一个奇怪的TypeError,说undefined不是一个函数,我不知道它来自哪里。

有谁知道问题是什么?我认为设置起来非常简单,但现在已经挣扎了几天。

应用程序文件夹结构:

karma.conf.js
package.json
/node_modules
/src
 |_ /frontend
      |_ /app
          |_ index.html
          |_ bower.json
          |_ /bower_components
          |_ /scripts
          |_ /styles
          |_ /views
/test
  |_ /frontend
       |_ /services

karma.conf.js:

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['mocha', 'chai'],


    // list of files / patterns to load in the browser
    files: [
        'src/frontend/app/bower_components/angular/angular.js',
        'src/frontend/app/bower_components/angular-route/angular-route.js',
        'src/frontend/app/bower_components/angular-mocks/angular-mocks.js',
        'src/frontend/**/*.js',
        'test/frontend/**/*.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-
preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // 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
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
     browsers: ['PhantomJS'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity
   })
 }   

gamesServiceTest.js:

describe('gamesServiceTest', function() {

    describe('gamesService', function() {
       it('should pass', function() {
          console.log('WOOO');
       }); 
    }); 
});

我可以从命令行运行此测试并通过:

matthew@ubuntu:~/Node/Video-Game-Website/test/frontend/services$ mocha gamesServiceTest.js 


  gamesServiceTest
    gamesService
WOOO
      ✓ should pass


  1 passing (8ms)

当我运行karma start时,我收到一个奇怪的错误:

matthew@ubuntu:~/Node/Video-Game-Website$ karma start
01 06 2017 17:59:57.847:WARN [karma]: No captured browser, open http://localhost:9876/
01 06 2017 17:59:57.860:INFO [karma]: Karma v1.7.0 server started at http://0.0.0.0:9876/
01 06 2017 17:59:57.862:INFO [launcher]: Launching browser PhantomJS with unlimited concurrency
01 06 2017 17:59:57.873:INFO [launcher]: Starting browser PhantomJS
01 06 2017 17:59:58.402:INFO [PhantomJS 2.1.1 (Linux 0.0.0)]: Connected on socket 2KeKaqM6r1COC3ugAAAA with id 3769272
PhantomJS 2.1.1 (Linux 0.0.0) ERROR
  TypeError: undefined is not a function
  at undefined

如何让Karma执行我的测试?提前感谢任何建议!

修改

添加package.json:

{                                                                               
  name: videogames,
  private: true,
  dependencies: {
    body-parser: ^1.17.1,
    express: ^4.14.0,
    mysql: ^2.12.0
  },  
  devDependencies: {
    chai: ^3.5.0,
    chai-http: ^3.0.0,
    karma: ^1.7.0,
    karma-chai: ^0.1.0,
    karma-mocha: ^1.3.0,
    karma-phantomjs-launcher: ^1.0.4,
    mocha: ^3.3.0
  },  
  engines: {
    node: >=0.10.0
  }
}

0 个答案:

没有答案