2013-08-09 26 views
52

ho definito il seguente servizio nella mia app angolare:Perché i ricevo errore ... inaspettata richiesta: GET/internalapi/citazioni

services.factory('MyService', ['Restangular', function (Restangular) { 
     return { 
      events : { loading : true }, 

      retrieveQuotes : function() { 
       return Restangular.all('quotes').getList().then(function() { 
        return { hello: 'World' }; 
       }); 
      } 
    }; 
}]); 

e sto scrivendo la seguente specifica per testarlo:

describe("MyService", function() { 

    beforeEach(module('MyApp')); 
    beforeEach(module("restangular")); 

    var $httpBackend, Restangular, ms; 

    beforeEach(inject(function (_$httpBackend_, _Restangular_, MyService) { 
     ms = MyService; 
     $httpBackend = _$httpBackend_; 
     Restangular = _Restangular_; 
    })); 


    it("retrieveQuotes should be defined", function() { 
     expect(ms.retrieveQuotes).toBeDefined(); 
    }); 

    it("retrieveQuotes should return array of quotes", function() { 

     $httpBackend.whenGET("internalapi/quotes").respond({ hello: 'World' }); 
     ms.retrieveQuotes(); 
     $httpBackend.flush(); 
    }); 

}); 

Ogni volta che esegue il test, il primo test viene superato ma la seconda prova produce l'errore:

Error: Unexpected request: GET /internalapi/quotes

Cosa sto sbagliando?

EDIT:

Si è scoperto che avevo configurato Restangular in questo modo ... RestangularProvider.setBaseUrl("/internalapi");. Ma stavo telefonando allo internalapi/quotes. Notare la mancanza del "/". Una volta aggiunta la barra /internalapi/quotes, tutto andava bene :)

+1

http://stackoverflow.com/questions/14761045/jasmine-tests-angularjs-directives-with-templateurl (non ho segnala come duplicato, perché i problemi sembrano diversi in una certa misura). –

+1

Ricorda di contrassegnare la domanda come risolta, in base alla modifica sembra che sia stata corretta. :) –

+0

Potrebbe essere ovvio per gli altri, ma nel caso non lo fosse, queste chiamate 'expectMETHOD' si aspettano che tu passi l'URL completo, non solo il percorso, se stai chiamando i servizi esterni. – kungphu

risposta

54

È necessario dire a $ httpBackend di aspettarsi una richiesta GET.

describe("MyService", function() { 

    beforeEach(module('MyApp')); 
    beforeEach(module("restangular")); 

    var Restangular, ms; 

    beforeEach(inject(function (_Restangular_, MyService) { 
     ms = MyService; 

     Restangular = _Restangular_; 
    })); 


    it("retrieveQuotes should be defined", function() { 
     expect(ms.retrieveQuotes).toBeDefined(); 
    }); 

    it("retrieveQuotes should return array of quotes", inject(function ($httpBackend) { 

     $httpBackend.whenGET("internalapi/quotes").respond({ hello: 'World' }); 

     //expect a get request to "internalapi/quotes" 
     $httpBackend.expectGET("internalapi/quotes"); 

     ms.retrieveQuotes(); 
     $httpBackend.flush(); 
    })); 

}); 

In alternativa si può mettere il respond() sul expectGET(). Preferisco inserire le mie istruzioni whenGET() in un beforeEach() in questo modo non è necessario definire la risposta all'interno di ogni test.

 //expect a get request to "internalapi/quotes" 
     $httpBackend.expectGET("internalapi/quotes").respond({ hello: 'World' }); 

     ms.retrieveQuotes(); 
     $httpBackend.flush(); 
+3

Sfortunatamente, anche se aggiungo '' '$ httpBackend.expectGET (" internalapi/quotes ");' '' , Ho fatto lo stesso errore. Altre idee? –

+0

Questo dovrebbe funzionare. Solo una supposizione, ma prova a iniettare il '$ httpBackend' direttamente nel tuo test. Aggiornerò il post per mostrarlo. –

+0

No, stessa cosa. Ho esaminato molti esempi e sono d'accordo, sembra che lo stia facendo correttamente, ma purtroppo non c'è gioia. –

17

Ho avuto lo stesso problema di voi ragazzi. La mia soluzione era aggiungere un '/' all'inizio del parametro URL di .expectGET. Usando il tuo esempio:

$httpBackend.expectGET("/internalapi/quotes").respond({ hello: 'world'}) 

Buona fortuna

Problemi correlati