2013-04-16 24 views
23

Sembra che stia avendo problemi a eseguire i miei test di unità Jasmine. Ho verificato che tutti i miei script vengano caricati impostando logLevel su LOG_DEBUG. Il mio test unitario è identico al test di servizio @https://github.com/angular/angular-seed/blob/master/test/unit/servicesSpec.js.Karma non esegue alcun test di unità

Inoltre, ho utilizzato Testacular (prima che fosse rinominato in Karma) e non ricordo di aver riscontrato questo problema. Mi sembra di far funzionare Chrome, ma devo premere manualmente un pulsante "Debug". Anche quando premo questo pulsante i miei test non funzionano.

dettagli di sistema:

  • di Windows 7
  • Nodo v0.10.4
  • Chrome 26.0.14
  • Karma 0.8.5 (installato con 3 avvertimenti - 2 perdita di precisione e una 'nessuna definizione per la funzione inline v8 :: Persistent v8 :: Persistent :: New (v8 :: Handle)')

Ecco il mio codice del modulo (Scripts/main.js):

angular.module('sb.services', []).value('version', '0.0.1').value('amplify', amplify); 
angular.module('sb.directives', []); 
angular.module('sb.filters', []); 
angular.module('sb.controllers', []).controller('SbController', [ 
    '$scope', 
    'amplify', 
    function ($scope, amplify) { 
     $scope.message = 'Hello World! (amplify exists?=' + !!amplify + ')'; 
    } 
]); 
angular.module('sb', [ 
    'sb.services', 
    'sb.directives', 
    'sb.filters', 
    'sb.controllers' 
]); 

Ecco la mia spec (Test/unità/servicesSpec.js):

'use strict'; 

describe('my services', function() { 
    beforeEach(module('sb.services')); 

    describe('version', function() { 
     it('should return current version', inject(function(version) { 
      expect(version).toEqual('0.0.1'); 
     })); 
    }); 
}); 

Ecco il mio file karma.conf.js:

// Karma configuration 
// Generated on Mon Apr 15 2013 20:56:23 GMT-0400 (Eastern Daylight Time) 


// base path, that will be used to resolve files and exclude 
basePath = ''; 


// list of files/patterns to load in the browser 
files = [ 
    JASMINE, 
    JASMINE_ADAPTER, 
    'Vendor/angular-1.0.6/angular.js', 
    'Vendor/angular-1.0.6/angular-*.js', 
    'Vendor/json2/json2.js', 
    'Vendor/jquery/jquery-1.8.2.js', 
    'Vendor/amplify/amplify.js', 
    'Scripts/main.js', 
    'Test/unit/*.js' 
]; 


// list of files to exclude 
exclude = [ 

]; 


// test results reporter to use 
// possible values: 'dots', 'progress', 'junit' 
reporters = ['progress']; 


// web server port 
port = 9876; 


// cli runner port 
runnerPort = 9100; 


// enable/disable colors in the output (reporters and logs) 
colors = true; 


// level of logging 
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG 
logLevel = LOG_WARN; 


// enable/disable watching file and executing tests whenever any file changes 
autoWatch = false; 


// Start these browsers, currently available: 
// - Chrome 
// - ChromeCanary 
// - Firefox 
// - Opera 
// - Safari (only Mac) 
// - PhantomJS 
// - IE (only Windows) 
browsers = ['Chrome']; 

junitReporter = { 
    outputFile: 'Test/out/unit.xml', 
    suite: 'unit' 
}; 


// If browser does not capture in given timeout [ms], kill it 
captureTimeout = 60000; 


// Continuous Integration mode 
// if true, it capture browsers, run tests and exit 
singleRun = false; 
+0

credo di essere piuttosto tardi con questo, ma penso che dovrete impostare sia 'singleRun' o' autoWatch' su true. – fodma1

risposta

11

La mia ultima risposta era errata (spostamento delle linee JASMINE e JASMINE_ADAPTER sotto le linee angular.js). Risolve questo particolare problema, ma creava altri problemi. Invece, quello che ho fatto per risolvere il problema è stato quello di indicare solo il file angolare prende in giro, invece di angular- *, in questo modo:

JASMINE, 
    JASMINE_ADAPTER, 
    'Vendor/angular-1.0.6/angular.js', 
    'Vendor/angular-1.0.6/angular-mocks.js', 
    'Vendor/json2/json2.js', 
    'Vendor/jquery/jquery-1.8.2.js', 
    'Vendor/amplify/amplify.js', 
    'Scripts/main.js', 
    'Test/unit/*.js' 
+0

Grazie per l'aiuto di rquinn, qualche idea sul perché avere tutti i file angolari JS inclusi ha causato che i test non siano stati eseguiti? – Darren

+5

È a causa del file dello scenario angolare, che viene utilizzato per il test end-to-end. Penso che ci sia un conflitto tra questo e il file dei mock angolari. –

+1

Grazie a questo mi ha aiutato, ho trovato potrebbe usare l'esclusione come sotto per saltare il file dello scenario. – Darren

13

In alternativa è possibile utilizzare escludere sezione nelle vostre karma.conf.js

exclude = [ 
    'Vendor/angular-1.0.6/angular-scenario.js' 
]; 
14

Ho appena avuto lo stesso problema e ho scoperto che dovevo impostare autoWatch = true prima che Karma eseguisse automaticamente i test.

6

Se si sta tentando di risolvere questo problema utilizzando JASMINE e JASMINE_ADAPTER questo non è più supportato (almeno su Karma versione 0.10.2).

utilizzare invece:

frameworks: ['jasmine'] 

nel file di configurazione Karma. Puoi leggere su questo here.

Ho anche trovato che nel mio array di file avevo impostato included: false per un modello. included viene utilizzato solo se si desidera includere manualmente i file Javascript (ad esempio se si utilizza require.js). Se tutti i test verranno caricati da quel modello verrà visualizzato un messaggio simile a:

PhantomJS 1.9.2 (Linux): Executed 0 of 0 ERROR (0.151 secs/0 secs) 

dal momento che il file contenente i test non è mai incluso. La rimozione di included: false dall'array di file per il mio modello ha risolto questo problema poiché il valore predefinito se non specificato non è specificato true.

7

Ho provato tutti sopra senza successo, fino a quando finalmente ...

Nel mio karma.conf.js ho rimosso i requirejs dipendenza, ad esempio:

frameworks: ['jasmine', 'requirejs'], 

a:

frameworks: ['jasmine'], 
1

In il mio caso singleRun è stato impostato su false

Soluzione: // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: true