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 :)
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). –
Ricorda di contrassegnare la domanda come risolta, in base alla modifica sembra che sia stata corretta. :) –
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