2015-11-23 11 views
5

Sto provando a testare la mia applicazione AngularJS usando Karma e Jasmine. Voglio prendere in giro il servizio $ http. Per questo, sto usando il metodo $ httpBackend. Di seguito è riportato il mio servizio che voglio provare:

angular.module('MyModule').factory('MyService', function($http, $log, $parse, $q, $timeout, $filter, MyOtherService1, MyOtherService2){ 
var service = {}; 
    service.getSomething = function(id){ 
    return $http.get('/somePath/subpath/' + id); 
    } 
}); 

Il mio test di unità per questo servizio è:

describe("myTest", function(){ 
    var myService, $httpBackend, scope, mockMyOtherService1, mockMyOtherService2; 

    var myResponse = 
    { 
     foo:'bar' 
    }; 

    beforeEach(module("MyModule")); 

    beforeEach(inject(function(_MyService_, $injector){ 

     $httpBackend = $injector.get('$httpBackend'); 
     myService = _MyService_; 
     scope = $injector.get('$rootScope').$new(); 
     mockMyOtherService1 = $injector.get('MyOtherService1'); 
     mockMyOtherService2 = $injector.get('MyOtherService2'); 

    })); 

    beforeEach(function(){ 
     //To bypass dependent requests 
     $httpBackend.whenGET(/\.html$/).respond(200,''); 
    }); 

    //If I uncomment the below afterEach block, the same error is shown at next line. 
    /*afterEach(function() { 
     $httpBackend.verifyNoOutstandingExpectation(); 
     $httpBackend.verifyNoOutstandingRequest(); 
    });*/ 

    //This test passes successfully 
    it("should check if service is instantiated", function() { 
     expect(myService).toBeDefined(); 
    }); 

    //This test passes successfully 
    it("should expect dependencies to be instantiated", function(){ 
     expect($httpBackend).toBeDefined(); 
    }); 

    //The problem is in this test 
    it("should get the getSomething with the provided ID", function() { 
     $httpBackend.whenGET('/somePath/subpath/my_123').respond(200,myResponse);    
     var deferredResponse = myService.getSomething('my_123'); 

     //The error is shown in next line. 
     $httpBackend.flush();  

     //If I comment the $httpBackend.flush(), in the next line, the $$state in deferredResponse shows that the Object that I responded with is not set i.e. it does not matches the 'myResponse'. 
     expect(deferredResponse).toEqual(myResponse); 

    }); 
}); 

Questo è un problema di emergenza e ho bisogno di aiuto per quanto riguarda la stessa nel più breve tempo possibile. Sarò molto grato per la tua risposta.

risposta

1

Il problema era che mi serviva di iniettare $ location nei miei file spec anche se non vengono iniettati nei servizi. Dopo l'iniezione, tutto ha funzionato bene! Spero che questo aiuti qualcuno che si blocca nella stessa situazione.

+1

Questo non ha funzionato per me: P –

+0

@TusharRaj Qual è il problema esatto che stai affrontando? Per favore indicami la tua domanda su StackOverflow – Jagrut

1

Otterrete una promessa dal vostro servizio. Quindi modificare il codice di prova per:

//The problem is in this test 
it("should get the getSomething with the provided ID", function (done) { 
    $httpBackend.expectGET('/somePath/subpath/my_123').respond(200,myResponse); 
    var deferredResponse = myService.getSomething('my_123'); 

    deferredResponse.then(function (value) { 
    expect(value.data).toEqual(myResponse); 
    }).finally(done); 

    $httpBackend.flush(); 
}); 
+0

Ho provato il codice ma dà lo stesso errore perché l'errore è sulla linea $ httpBackend.flush(). Non viene eseguito dopo quella linea stessa. Se commento la riga $ httpBackend.flush(), allora funziona. Ma ho bisogno del blocco $ httpBackend.flush() e afterEach(). – Jagrut

+0

Ho aggiornato la risposta, spostato la chiamata flush() alla fine. Ora funziona sulla mia macchina. Puoi riprovare con il codice aggiornato? – ogugger

+0

Ancora lo stesso errore! Penso di trascurare qualcosa ma non riesco a scoprirlo. Anche se non scrivo alcuno() blocco, afterEach() dovrebbe funzionare ma non funziona anche! Quindi ho dovuto commentare e procedere ulteriormente. Ma anche allora sono bloccato a $ httpBackend.flush()! – Jagrut

1

Recentemente ho riscontrato questo problema durante l'aggiornamento di un progetto da Angular 1.2 a 1.4. Il codice di prova sembrava qualcosa di simile:

it('should call /something', function(){ 
    httpBackend.expectGET('/something').respond(200); 
    scope.doSomething(); 
    httpBackend.flush(); 
}); 

L'errore è stato l'infdig ultimi 10 iterazioni. È stato causato invocando il metodo .flush(). Ho capito che questo è apparentemente perché non c'erano promesse in sospeso create entro doSomething().

Una volta aggiunta una promessa da qualche parte entro doSomething() o metodi interni, il problema infdig è andato via.

Sospetto - e questa è una speculazione al 100% quindi non lasciare che ciò influisca sul tuo sviluppo - questo è perché httpBackend fa qualche trucco per aspettare promesse, il che probabilmente implica digerire ripetutamente fino a quando non c'è un cambiamento. Dal momento che non ci sono promesse, non ci sono cambiamenti - digest infinito.

Problemi correlati