CoffeeScript Code UnitTest AngularJS - Jasmine - Karma

时间:2016-06-22 09:54:53

标签: angularjs coffeescript karma-jasmine

我正在尝试将单元测试写入我使用CoffeeScript编写的Ionic项目的控制器和服务。我选择使用Karma和Jasmine。问题是它不解释coffeeScript。这就是我使用这个插件karma-coffee-preprocessor以便它成为可能的原因。我不知道我做得对不对。

我用命令安装了插件; npm install --save-dev karma-coffee-preprocessor 然后,我按如下方式配置了我的测试配置文件:

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

    basePath: '',
    frameworks: ['jasmine'],
    files: [
        '../bower_components/angular/angular.js',
        '../bower_components/angular-ui-router/release/angular-ui-router.js',
        '../bower_components/angular-mocks/angular-mocks.js',
        '../src/scripts/**/*.coffee',
        'unit-tests/**/*.js'
    ],

    exclude: [
    ],

    preprocessors: {
        '../src/scripts/**/*.coffee': ['coffee']
    },

    coffeePreprocessor: {
      options: {
        bare: true,
        sourceMap: false
      },
      transformPath: function(path) {
        return path.replace(/\.coffee$/, '.js')
      }
    },

    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_DEBUG,
    autoWatch: true,
    browsers: ['PhantomJS'],
    singleRun: false,
    concurrency: Infinity
  })
}

但是当我尝试开始测试时,我遇到了这个错误:

 Can not load "coffee", it is not registered!
  Perhaps you are missing some plugin?
/opt/node/lib/node_modules/karma/node_modules/di/lib/injector.js:9
      throw error('No provider for "' + name + '"!');
      ^

Error: No provider for "framework:jasmine"! (Resolving: framework:jasmine)
    at error (/opt/node/lib/node_modules/karma/node_modules/di/lib/injector.js:22:12)
    at Object.parent.get (/opt/node/lib/node_modules/karma/node_modules/di/lib/injector.js:9:13)
    at get (/opt/node/lib/node_modules/karma/node_modules/di/lib/injector.js:54:19)
    at /opt/node/lib/node_modules/karma/lib/server.js:138:20
    at Array.forEach (native)
    at Server._start (/opt/node/lib/node_modules/karma/lib/server.js:137:21)
    at invoke (/opt/node/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15)
    at Server.start (/opt/node/lib/node_modules/karma/lib/server.js:102:18)
    at Object.exports.run (/opt/node/lib/node_modules/karma/lib/cli.js:243:26)
    at Object.<anonymous> (/opt/node/lib/node_modules/karma/bin/karma:3:23)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:146:18)

任何人都可以帮助我吗? 我无法进行这些单元测试,而且我不知道我是否以正确的方式做事。

1 个答案:

答案 0 :(得分:0)

首先,您需要通过npm安装jasmine框架并将其添加到karma配置文件中的frameworks集合中:

$ npm install karma-jasmine --save-dev

然后添加:

frameworks: ['jasmine']

到karma配置文件。