2013-05-23 9 views
5

Voglio testare la risorsa angularjs.

'use strict'; 

/** 
* AddressService provides functionality to use address resource in easy way. 
* 
* This is an example usage of method: 
* 
* `get`: 
* 
    var a = AddressService.get({id: '1'}, 
     function (data) { 
      // Work here with your resource 
     } 
    ); 
* 
*/ 
App.factory('AddressService', function ($resource, $rootScope) { 
    var url = [ 
      $rootScope.GLOBALS.API_PATH, 
      $rootScope.GLOBALS.API_VERSION, 
      'models/address/:id' 
     ].join('/'), 

     actions = { 
      'get': { 
       method: 'GET', 
       params: {id: '@id'} 
      } 
     }; 
    return $resource(url, {}, actions); 
}); 

ho creato il test:

'use strict'; 

var $httpBackend; 

describe('Service: AddressService', function() { 

    beforeEach(module('myApp')); 

    beforeEach(inject(function ($injector) { 
     var url_get = 'api/v1/models/address/5'; 

     var response_get = { 
      address_line_1: '8 Austin House Netly Road', 
      address_line_2: 'Ilford IR2 7ND' 
     }; 

     $httpBackend = $injector.get('$httpBackend'); 

     $httpBackend.whenGET(url_get).respond(response_get); 

    })); 

    describe('AddressService get test', function() { 
     it('Tests get address', inject(function (AddressService) { 
      var address = AddressService.get({ id: '5'}); 
      $httpBackend.flush(); 
      expect(address.address_line_1).toEqual('8 Austin House Netly Road'); 
      expect(address.address_line_2).toEqual('Ilford IR2 7ND'); 
     })); 
    }); 
}); 

Io non sono esperto con angolare molto bene. Ho impostato il gelsomino in karma.config.js. AngularJS v1.0.6 Yoeman e Grunt gestiscono il progetto.

cerco grunt test

Running "karma:unit" (karma) task 
INFO [karma]: Karma server started at http://localhost:8080/ 
INFO [launcher]: Starting browser Chrome 
INFO [Chrome 27.0 (Linux)]: Connected on socket id RFFUY5bW8Hb5eTu0n-8L 
Chrome 27.0 (Linux) Service: AddressService AddressService get Tests get address FAILED 
     Error: Unexpected request: GET views/home.html 
     No more request expected 
      at Error (<anonymous>) 
      at $httpBackend (/home/bart/y/projects/x/frontend/app/components/angular-mocks/angular-mocks.js:910:9) 
      at sendReq (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:9087:9) 
      at $http (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:8878:17) 
      at Function.$http.(anonymous function) [as get] (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:9021:18) 
      at $q.when.then.then.next.locals (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:7394:34) 
      at wrappedCallback (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:6797:59) 
      at wrappedCallback (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:6797:59) 
      at /home/bart/y/projects/x/frontend/app/components/angular/angular.js:6834:26 
      at Object.Scope.$eval (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:8011:28) 
     Error: Declaration Location 
      at window.jasmine.window.inject.angular.mock.inject (/home/bart/y/projects/x/frontend/app/components/angular-mocks/angular-mocks.js:1744:25) 
      at null.<anonymous> (/home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:32:30) 
      at null.<anonymous> (/home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:31:5) 
      at /home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:5:1 
.................................................................. 
Chrome 27.0 (Linux): Executed 67 of 67 (1 FAILED) (0.343 secs/0.179 secs) 
Warning: Task "karma:unit" failed. Use --force to continue. 

Se rimuovo $httpBackend.flush() in prova. Il test sta passando. Ma sto ottenendo undefined da address. Ho visto esempi: example tutti usano il flush() Ma solo ottengo sciocco eccezione: Error: Unexpected request: GET views/home.html

views/home.html è la mia opinione in directory del progetto. Non ho idea di come risolvere il mio problema non sono riuscito a trovare alcuna soluzione. Mi manca il punto in qualche modo?

Qualcuno può vedere un errore nel mio codice? Tutti i suggerimenti saranno apprezzati.

EDIT

Ho scoperto che ho bisogno di usare questo:

$httpBackend.when('GET', /\.html$/).passThrough(); 

Ma un altro problema è che sto ottenendo: soluzione

TypeError: Object #<Object> has no method 'passThrough' 

risposta

8

ho finalmente trovato:

Ho provato a seguire questo: other post

così ho aggiunto $templateCache:

'use strict'; 

var $httpBackend; 

describe('Service: AddressService', function() { 

    beforeEach(module('myApp')); 

    beforeEach(inject(function ($injector, $templateChache) { 
     $templateCache.put('views/home.html', '.<template-goes-here />'); 
     var url_get = 'api/v1/models/address/5'; 

     var response_get = { 
      address_line_1: '8 Austin House Netly Road', 
      address_line_2: 'Ilford IR2 7ND' 
     }; 

     $httpBackend = $injector.get('$httpBackend'); 

     $httpBackend.whenGET(url_get).respond(response_get); 

    })); 

    describe('AddressService get test', function() { 
     it('Tests get address', inject(function (AddressService) { 
      var address = AddressService.get({ id: '5'}); 
      $httpBackend.flush(); 
      expect(address.address_line_1).toEqual('8 Austin House Netly Road'); 
      expect(address.address_line_2).toEqual('Ilford IR2 7ND'); 
     })); 
    }); 
}); 
+2

Perché il templateCache è importante?Sembra che non abbia nulla a che fare con un servizio API. –

+5

Sono d'accordo. Questo sembra un terribile trucco per ignorare il problema. Sto incontrando gli stessi problemi ora e non sono sicuro del perché questo sia il caso ... – Pwnna

+2

Questi sono test unitari e devi estrarre unità specifiche (servizio in questo caso) per testare. Rimuovi tutte le altre dipendenze anche dai modelli che vengono semplicemente derisi qui. Non so come sia possibile non avere alcuna idea del flusso angolare del core. E questa domanda non è ancora chiusa. –

4

l'errore è causato dal tuo test tentando di caricare la pagina principale per la vostra applicazione. Poiché non hai detto al test di aspettarsi questa chiamata al server, viene restituito un errore. Vedere la documentazione per $httpBackend per chiarimenti su questo punto.

La soluzione alternativa a $ templateCache è progettata per le direttive di test delle unità e non altro. Tu eri abbastanza vicino con:

$httpBackend.when('GET', /\.html$/).passThrough(); 

Dal momento che non c'è bisogno di fare qualsiasi cosa con il file del modello attuale si tratta di un lavoro sicuro e semplice in giro per aggiungere questo al blocco beforeeach().

$httpBackend.whenGET(/\.html$/).respond(''); 

Questo ti impedisce di ottenere l'errore TypeError che avevi.

TypeError: Object #<Object> has no method 'passThrough' 

Ho avuto lo stesso problema nel mio test di unità dopo l'aggiornamento a una nuova versione di JS angolare e l'utilizzo di respond('') ha funzionato bene.

+1

Non è possibile invece evitare il servizio $ http che effettua tale richiesta alla pagina principale dell'applicazione? – Stephane

Problemi correlati