【问题标题】:Angular JS tests with Jasmine and PhantomJS, 0 Error使用 Jasmine 和 PhantomJS 进行 Angular JS 测试,0 错误
【发布时间】:2015-12-03 03:07:29
【问题描述】:

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: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
            '../scripts/bower_components/angularjs/angular.js',
            '../scripts/bower_components/angular-mocks/angular-mocks.js',
            '../scripts/app.js',
            '../scripts/11.js',
         
            '../scripts/controllers/*.js',
            '../scripts/directives/*.js',
            '../scripts/services/*.js',
            'controllers/controllersTests.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', 'PhantomJS_custom'],

    customLaunchers: {
      'PhantomJS_custom': {
        base: 'PhantomJS',
        options: {
          windowName: 'my-window',
          settings: {
            webSecurityEnabled: false
          },
        },
        flags: ['--load-images=true'],
        debug: false
      }
    },

    phantomjsLauncher: {
      // Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
      exitOnResourceError: true
    },
    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  })
}

我需要测试控制器的代码,但我看不到正确的结果,代码如下: “幻灯片”数组长度 = 4;但在测试中我写了“toBe(2)”,我看到了:

PhantomJS 1.9.8 (Linux 0.0.0):执行 0 of 0 错误(0.035 秒 / 0 秒)

为什么我看到 0 个错误,如果我期望 2,但数组长度是 4 ???

app.controller('mainCtrl',['$scope', function($scope){
  $scope.slide = [1, 2, 3, 4];
}]);
describe('Tests Controllers', function() {
  beforeEach(module('app'));

  var $controller;

  beforeEach(inject(function(_$controller_, $rootScope){
    $controller = _$controller_;

    it('check slides length, it should be 4', function() {
      var $scope = {};
      var controller = $controller('mainCtrl', { $scope: $scope });
      expect($scope.slide.length).toBe(2);
    });
  }));
});

【问题讨论】:

  • 您的测试文件甚至没有被执行,似乎是设置中的一个问题。这是您对应用程序的第一次测试吗? Karma 配置是否正确?
  • 是的,这是我的第一次测试。我已经附加了业力配置。
  • 你能分享你的 Karma 配置文件吗?
  • 确定,完成,你可以看到它!
  • 哦,我知道可能是什么问题了。您不应该将it 块放在beforeEach 中,它们必须在同一级别。您必须根据您的代码示例将it 放在第二个beforeEach 之后,而不是放在里面。

标签: angularjs unit-testing phantomjs karma-jasmine angular-mock


【解决方案1】:

当 Karma 找不到您的测试并显示 Executed 0 of 0 ERROR 时,导致此行为的最常见原因是:

  • karma.conf.jsfiles:[] 选项中的测试文件/文件夹的路径错误
  • 测试文件/文件夹中缺少规范(it 块),因此 Karma 没有什么可执行的。如果规范在测试文件中放置不当,也可能发生这种情况,例如在您的情况下,您已将 it 放入 beforeEach,但 Jasmine 不支持它。我们的想法是将它们放在同一水平上。 it 规范可以单独存在于全局范围内,也可以直接存在于 describe 套件块内。

【讨论】:

  • 如果故意设置 0 个测试,有没有办法告诉 Karma 不要将这个 0 of 0 视为错误?
  • @core,我猜这是设计使然,看看source code here - 如果没有测试,那么number of success tests 为0,这是假的,所以它被认为是一个错误。也许创建一个虚拟规范对您有用?
  • @core 您可以将failOnEmptyTestSuite: false 添加到您的 karma.conf.js
猜你喜欢
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 2023-01-13
  • 2020-12-11
相关资源
最近更新 更多