0

角JS測試和PhantomJS,0錯誤

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 ;但在測試I寫 「砥(2)」 和我看到:

PhantomJS 1.9.8(Linux的0.0.0)○:執行0 ERROR(0.035秒/ 0秒)

爲什麼我看到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); 
    }); 
    })); 
}); 
+0

您的測試文件甚至沒有執行,似乎是在安裝程序中的問題。這是你的第一個測試你的應用程序? Karma配置是否正確? –

+0

是的,這是我的第一次測試。我已經附加了karma配置。 – trigger

+0

你能分享你的Karma配置文件嗎? –

回答

3

當噶無法找到你的測試,並顯示Executed 0 of 0 ERROR,導致這種現象的最流行的原因是:在

  • 壞路徑測試文件/文件夾中karma.conf.jsfiles:[]選項
  • 缺少測試文件/文件夾中的規格(it塊),因此Karma無法執行。如果規格放置在測試文件中不恰當,也可能會出現這種情況,例如,您已將it置於beforeEach之內,但Jasmine不支持。這個想法是把他們放在同一個水平上。 it規範可以在全球範圍內單獨生活,也可以在describe套房區塊內單獨生活。
+0

如果故意有0個測試,有沒有辦法告訴Karma不要把這個0作爲一個錯誤? – core

+0

@core,我想這是設計,看看[源代碼](https://github.com/karma-runner/karma/blob/master/lib/browser.js#L137) - 如果有沒有測試,然後[成功測試數量](https://github.com/karma-runner/karma/blob/master/lib/browser_result.js#L16)是0,這是虛假的,所以它被認爲是一個錯誤。也許創建一個虛擬規範將爲你工作? –