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

我需要測試控制器的代碼,但是我看不到正確的結果,代碼如下:“ slide” array length = 4; 但在測試中,我寫了“ toBe(2)”,我看到:

PhantomJS 1.9.8(Linux 0.0.0):執行0的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找不到測試並顯示Executed 0 of 0 ERROR ,導致這種現象的最流行原因是:

  • files:[]選項中karma.conf.js中測試文件/文件夾的錯誤路徑
  • 缺少規范( it在測試文件/文件夾塊),所以噶無關的執行。 如果規范沒有適當地放置在測試文件中,也可能發生這種情況,例如您將it放在beforeEach ,但是Jasmine不支持它。 想法是將它們置於同一級別。 it可以在全局范圍內單獨存在,也可以在describe套件塊內直接存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM