2014-08-29 7 views
10

La mia applicazione sembra funzionare bene. Ho iniziato a scrivere casi di test e ho ottenuto il temuto iniettore già creato, non posso registrare un modulo! Errore.Impossibile capirci "L'iniettore è già stato creato, non è possibile registrare un modulo!" error

Ecco il mio codice di test. Queste sono le uniche linee nel file.

'use strict'; 

var fac, 
    osf, 
    obff; 

beforeEach(module("myApp")); 

beforeEach(inject(function (OrderSashingFactory) { 
    fac = OrderSashingFactory; 
})); 

Davvero non so da dove andare, sto andando in circolo.

EDIT - ecco il mio file karma.conf.js. Ho altri test che funzionano bene.

// Karma configuration 
// Generated on Mon Aug 25 2014 21:08:59 GMT-0400 (Eastern Daylight Time) 

module.exports = function (config) { 
    config.set({ 

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


     // frameworks to use 
     frameworks: ['mocha', 'chai', 'sinon'], 


     // list of files/patterns to load in the browser 
     files: [ 
      'app/bower_components/angular/angular.js', 
      'app/bower_components/angular-route/angular-route.js', 
      'app/bower_components/angular-mocks/angular-mocks.js', 
      'app/js/*.js', 
      'app/test/js/*.js', 
      'app/partials/**/*.html' 
     ], 

     preprocessors: { 
      'app/partials/**/*.html' : 'html2js' 
     }, 

     ngHtml2JsPreprocessor: { 
      // strip app from the file path 
      stripPrefix: 'app/' 
     }, 

     // list of files to exclude 
     exclude: [ 

     ], 

//  plugins: [ 
//   'karma-mocha', 
//   'karma-chrome-launcher' 
//  ], 


     // test results reporter to use 
     // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' 
     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, currently available: 
     // - Chrome 
     // - ChromeCanary 
     // - Firefox 
     // - Opera (has to be installed with `npm install karma-opera-launcher`) 
     // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`) 
     // - PhantomJS 
     // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`) 
     browsers: ['Chrome'], 


     // 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

Puoi mostrarci il tuo karma.conf.js se ce ne sono? – axelduch

+0

@aduch postato ora. –

+4

Non hai terminato il test in 'describe ('Foo',/* la tua funzione con il codice dato * /);' potrebbe essere il problema? – axelduch

risposta

22

Non hai avvolto il test in un describe, sarebbe simile a questa:

describe('MyTestName', function() { 
    'use strict'; 

    var fac, 
     osf, 
     obff; 

    beforeEach(module("myApp")); 

    beforeEach(inject(function (OrderSashingFactory) { 
     fac = OrderSashingFactory; 
    })); 
}); 
+0

Sono una tale ciambella. . . Grazie – jolySoft

5

caso si combinino le chiamate verso module('someApp') e inject($someDependency) otterrete questo errore.

Tutte le chiamate a module('someApp') devono essere effettuate prima delle chiamate a inject($someDependency).

1

Per aggiungere sopra a questa risposta da @axelduch, ho riscontrato un problema in cui un altro file non avrebbe descritto in, ed è quello che stava causando il mio iniettore già creato, impossibile registrare un modulo!

Se si esegue il test con molti file collegati come questo (questo sta usando requirejs)

define(["require", "exports", "./test1.tests", "./test2.tests"], function (require, exports, Test1, Test2) { 
    describe("Test Module", function() { 
     Test1.tests(); 
     Test2.tests(); 
    }); 
}); 

Poi assicurare ad ogni test che vengono eseguiti in questo descrivono qui contiene il proprio metodo descrivere a loro. Così, per esempio, il file Test1 sarebbe simile a questa

function tests() { 
     describe("Test directive", function() { 
      beforeEach(function() { 
      }); 
      afterEach(function() { 
      }); 
     }); 
    } 

Assicurarsi che il file tutti i test collegate nell'ambito di applicazione del test ha il proprio metodo descrivere, altrimenti non si può sapere, ma è un altro test che rende il tuo test unitario fallisce ... e ci può volere molto tempo per realizzarlo.

0

Questo può anche significare di avere qualcosa di simile al seguente:

inject(function ($compile, $rootScope, $document, $timeout) { 
    // Code processing inject 
}); 

module(function($provide) { 
    // Provide code 
}) 

che è sbagliato, il test runner non vi permetterà di farlo in questo ordine come la sua iniezione già occupato e si sono passato alla fase per la configurazione fornisce.

Il modo corretto, naturalmente, è:

module(function($provide) { 
    // Provide code 
}) 

inject(function ($compile, $rootScope, $document, $timeout) { 
    // Code processing inject 
}); 

garantire lo avete nel giusto ordine.

Problemi correlati