简体   繁体   English

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

[英]Angular JS tests with Jasmine and PhantomJS, 0 Error

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

I need to test code of controllers, but i can't see right result, code below: "slide" array length = 4; 我需要测试控制器的代码,但是我看不到正确的结果,代码如下:“ slide” array length = 4; but in test i write "toBe(2)" and i see: 但在测试中,我写了“ toBe(2)”,我看到:

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

Why i see 0 errors, if I expect 2, but array length is 4 ??? 为什么我看到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);
    });
  }));
});

When Karma can't find your tests and displays Executed 0 of 0 ERROR , the most popular reasons which lead to this behavior are: 当Karma找不到测试并显示Executed 0 of 0 ERROR ,导致这种现象的最流行原因是:

  • bad path to a test file/folder in karma.conf.js in the files:[] option files:[]选项中karma.conf.js中测试文件/文件夹的错误路径
  • missing specs ( it blocks) in the test file/folder, so Karma has nothing to execute. 缺少规范( it在测试文件/文件夹块),所以噶无关的执行。 It may occur also if specs are placed inappropriately within a test file, like in your case you've put it inside beforeEach , but Jasmine does not support it. 如果规范没有适当地放置在测试文件中,也可能发生这种情况,例如您将it放在beforeEach ,但是Jasmine不支持它。 The idea is to put them on the same level. 想法是将它们置于同一级别。 it spec can live separately in the global scope or right within describe suite blocks. it可以在全局范围内单独存在,也可以在describe套件块内直接存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM