E2E测试Angular与Karma - ng-scenario'模块未定义'

时间:2014-04-08 11:36:47

标签: node.js angularjs karma-runner

我一直试图用Karma进行一些e2e测试。 它根本不适合我。

现在我收到以下错误:

    Firefox 28.0.0 (Windows 7) ERROR
  ReferenceError: module is not defined
  at C:/MYPATH/Test/node_modules/karma-ng-scenario/lib/ind
ex.js:12

Firefox 28.0.0 (Windows 7) ERROR
  ReferenceError: browser is not defined
  at C:/MYPATH/Test/e2e/scenarios.js:12

我的配置文件如下所示:

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

    basePath : './',

    frameworks: ['ng-scenario'],

    files : [
    './node_modules/karma-ng-scenario/lib/*.js',
      './e2e/*.js'
    ],

    autoWatch : true,

    singleRun : true,

    browsers : ['Firefox'],

    plugins : [
            'karma-ng-scenario',
            'karma-chrome-launcher',
            'karma-firefox-launcher'
            ],

    junitReporter : {
      outputFile: 'test_out/unit.xml',
      suite: 'unit'
    },

    urlRoot : '/__karma/',

    proxies : {
      '/public/' : 'http://localhost:8080'
    }

  });
};

我的方案文件只是测试以查看基本路径是否重定向。 我已经做了很多搞乱npm到目前为止,最近“npm install karma-ng-scenario --save-dev”但不幸的是没有运气。

3 个答案:

答案 0 :(得分:0)

files:[...]数组中,您必须指定包含要编写测试的实际代码的所有文件,如果您的模块使用其他模块,则应添加模块所有模块的文件取决于。

假设您正在测试'someModule'模块,您必须包含'someModule'控制器,视图,服务,指令和其他模块(如果您的模块依赖于它们)

注意:假设您的文件位于特定目录中

files: [
  ...       //angularjs, angular-mocks, karma, protractor and other files you need
  ...                                
  'scripts/someModule/controllers/*.js',
  'scripts/someModule/services/*.js',
  'scripts/someModule/directives/*.js',
  'views/someModule/*.html'
]

或只是

files: [
   ...       //angularjs, angular-mocks, karma, protractor and other files you need
   ...
   'scripts/someModule/**/*.js',
   'views/someModule/*.html'
]

并确保在文件数组中安装并包含您所依赖的所有测试库(即angularjs,angular-mocks,protrator,karma等)

答案 1 :(得分:0)

files[]指定 angular.js 的路径作为第一个,然后使用任何其他角度模块(angular-mocks.js,angular-resource.js,angular-cookies .js),然后是你使用的任何库,然后是你自己的代码。

答案 2 :(得分:0)

所以我最终得到的E2E测试配置就是:

// Karma configuration
// Generated on Fri Apr 11 2014 02:35:20 GMT+0800 (China Standard Time)

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

    // base path, that will be used to resolve files and exclude
    basePath: '..',


    // frameworks to use
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
    ANGULAR_SCENARIO,
    ANGULAR_SCENARIO_ADAPTER,
      'test/e2e/*.js',
    ],


    // list of files to exclude
    exclude: [

    ],


    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    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, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    //browsers: ['PhantomJS', 'Firefox', 'Chrome'],
    browsers: ['Firefox', 'Chrome'],



    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


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

    proxies : {
    "/": "http://localhost:8080"
    },

    urlRoot : "/__karma/"
  });
};

似乎运行端到端测试的更好解决方案是使用量角器。 NG-SCENARIO已经过折旧并会引发警告,但仍应该运行。 https://github.com/angular/protractor