使用Jasmine和PhantomJS进行Angular JS测试,0错误

时间:2015-09-07 14:12:50

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

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
  })
}

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

PhantomJS 1.9.8(Linux 0.0.0):执行0 of 0 ERROR(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);
    });
  }));
});

1 个答案:

答案 0 :(得分:3)

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

  • karma.conf.js选项
  • files:[]中测试文件/文件夹的错误路径
  • 缺少测试文件/文件夹中的规范(it块),因此Karma无需执行任何操作。如果规范在测试文件中被不适当地放置,也可能发生,例如您将it置于beforeEach内,但Jasmine不支持它。我们的想法是将它们放在同一水平上。 it规范可以在全局范围内单独生存,也可以直接在describe套件块中生效。