2013-09-10 18 views
10

Ho un servizio, 'Inputs', definito nel modulo 'Puts', che dipende da un secondo servizio, 'InputCreator'. Devo interrompere il servizio InputCreator per testare il servizio Inputs.Iniezione delle dipendenze del servizio angolare fittizio

Come ho capito il answer here, dovrei creare un modulo contenente il mio stub service, quindi creare un nuovo modulo 'Test', specificando il modulo sotto test e quindi il modulo stub come dipendenze. E poi tira il servizio dall'iniettore. In questo modo:

beforeEach(function() { 

    angular.module.('Puts'); // contains the service 'Inputs' 

    angular.module('Mocks',[]) 
    .service('InputCreator',function(){ 
     var mockInputs = { 
     //stubbed behaviour goes here 
     }; 
     return mockInputs; 
    }); 
    }); 

    angular.module('Test',['Puts', 'Mocks']; 

    inject(function($injector){ 
    Inputs = $injector.get('Inputs'); 
    }); 
}); 

Tuttavia, la funzione di iniettore risponde con 'Unknown InputsProvider < - Ingressi'.

Dove sono andato fuori strada?

Grazie!

risposta

21

Dopo averlo capito, ho pensato di rispondere alla mia stessa domanda. Il grosso errore sopra stava usando angular.module piuttosto che angular.mock.module, che è convenienza referenziato come modulo da angular-mock. Non sono affatto la stessa cosa!

Inoltre, è sufficiente inizializzare il servizio di simulazione con angular.mock.module, a patto che lo si faccia prima di inizializzare il modulo in prova. Non è necessario per questo business 'avvolgere i moduli in un terzo modulo' come suggerito nella domanda collegata sopra. Vale a dire:

describe("Test Service", function() { 
    var TestService, getvaluestub; 

    beforeEach(function() { 

    // create mock service 
    var mock = {getvalue:function(){}} 

    angular.module('dependencymodule',[]) 
     .service('dependencyservice',function() { 
     return mock; 
     }); 

    //mock the function we are stubbing, (that, in this case, returns value 4) 
    getvaluestub = sinon.stub(mock,'getvalue')returns(4); 

    //instantiate your mock service 
    module('dependencymodule'); 

    //instantiate the module of the service under test, 
    //that depends on 'dependencyservice' mocked above 
    //(ie - testmodule includes the  service 'testservice') 
    module('testmodule'); 

    //inject your test service for testing 
    inject(function ($injector) { 
     TestService = $injector.get('testservice'); 
    }) 

    //tests go here..... 

Se il modulo di dipendenza esiste già, si potrebbe o ancora fare tutto quanto sopra, o si può acquistare il servizio dal $ iniettore, inserire le spie e stub, e> quindi < un'istanza servizio sotto test. È importante che le spie/stub siano impostate> prima dello < il servizio dipendente sia istanziato, o verrà istanziato senza di esse. Assomiglia a questo:

describe("Test Service", function() { 
    var TestService, DependencyService, getvaluestub; 

    beforeEach(function() { 

    // these modules are specified in the application 
    module('dependencymodule'); 
    module('testmodule'); 

    inject(function ($injector) { 
     DependencyService = $injector.get('testservice'); 

     getvaluestub = sinon.stub(DependencyService,'getvalue').returns(4); 

     OtherService = $injector.get('otherservice'); 
    }) 
    }); 

// test go here 

Quindi, ecco fatto. Si spera che questo sia utile a qualcuno che cerca "Iniezione di mock in servizi angolari".

+0

Grazie mille per questo! Ecco un altro esempio di test di un servizio, controller e filtro usando stub per il servizio. https://gist.github.com/clouddueling/11188718 –

Problemi correlati