是否可以使用Karma / Jasmine来填充或不填充下划线?

时间:2014-04-10 17:22:14

标签: unit-testing backbone.js underscore.js jasmine karma-runner

如何让Karama与骨干一起玩?我看到的所有文档都不需要再填充任何内容,但无论我是否做错,都会出错。 Karma托管我的文件并找到所有deps。我正在测试Backbone应用程序,不是Angular应用程序。加载全局_似乎是当前的主要问题。

我在RequireJSConfig.js中尝试了这个垫片(从Karma配置加载),我也试过了。

        ...
        shim: {

           'underscore':{
            exports:'_'
           }
        ...

我已确认通过在调试模式下运行Karma来解析实际文件:

DEBUG [watcher]: Resolved files:
    C:/Users/vspazzy/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js
    C:/Users/vspazzy/AppData/Roaming/npm/node_modules/karma-jasmine/lib/adapter.js
    C:/workspace/das-web/src/test/RequireJSConfig.js
    C:/workspace/das-web/src/main/resources/js/lib-clean/backbone-1.1.2.min.js
    C:/workspace/das-web/src/main/resources/js/lib-clean/jquery-2.1.0.js
    C:/workspace/das-web/src/main/resources/js/lib-clean/require-2.1.1.full.js
    C:/workspace/das-web/src/main/resources/js/lib-clean/underscore-1.5.2.min.js
    ...

这是我的Karma.conf.js:

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

    basePath: 'src',        

    frameworks: ['jasmine'],    

    reporters: ["coverage"],

    preprocessors: {
      "**/*.js": "coverage"
    },

    files: [
        'test/KarmaUnitConfig.js',    

        // require, backbone, jquery and underscore
        {pattern: 'main/resources/js/lib-clean/*.js', watched: true, included: true, served: true},

        // unit test helper
        {pattern: 'test/lib/testr.js', watched: true, included: true, served: true},

        //the unit tests
        {pattern: 'test/js/unit/**/*.js', watched: true, included: true, served: true},
        {pattern: 'test/js/unit/*.js', watched: true, included: true, served: true},
        {pattern: 'test/js/helpers/*.js', watched: true, included: true, served: true},

        // the app
        {pattern: 'main/**/*.js', watched: true, included: true, served: true},

        // the stubs
        {pattern: 'test/js/stubs/**/*.js', watched: true, included: true, served: true}

    ],


    // list of files to exclude
    exclude: [
        //crappy cluttered lib folder
        'main/resources/js/lib/*.js',

        // avoid minified (it is  a package of whole application)
        'main/app.min.js',

        //ignore the old test html
        'main/test/integration.html',
        'main/test/integration.min.html',
        'main/test/unit.html',
        'main/test/unit.min.html',
        'main/test/unit-headless.html'

    ],

    reporters: ['progress'],

    // web server port
    port: 9876,

    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_DEBUG,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,

    browsers: ['Chrome'],

    singleRun: false,

      /*
      assuming these are deprecated because these are bundled now
      plugins: [
          'karma-requirejs',
          'karma-jasmine',
          'karma-chrome-launcher',
          'karma-firefox-launcher'
      ]*/

  });
};

所以,此时Karma在运行测试时会抛出此错误:

Uncaught TypeError: Cannot call method 'each' of undefined
at http://localhost:9876/base/main/resources/js/lib-clean/backbone-1.1.2.min.js?69870e4d6c4da615282785f4b175f2dd9920d66a:219

- >在骨干这条线上

 _.each(listenMethods, function(implementation, method) {

0 个答案:

没有答案