2014-09-10 14 views
5

Sto provando a eseguire alcuni test con jasmine per un servizio AngularJS che ho creato per Spotify. Ma continuo a ricevere un errore con i miei test quando testare le promesse.

Il mio test è attualmente in questo modo:

describe('Spotify.search', function() { 
    var $httpBackend; 
    var $rootScope; 
    var Spotify; 
    var api = 'https://api.spotify.com/v1'; 

    beforeEach(inject(function(_Spotify_, _$httpBackend_, _$rootScope_) { 
    Spotify = _Spotify_; 
    $httpBackend = _$httpBackend_; 
    $rootScope = _$rootScope_; 
    jasmine.getJSONFixtures().fixturesPath='base/test/mock'; 
    })); 

    it('should return an array of artists', function() { 
    $httpBackend.when('GET', api + '/search?q=Nirvana&type=artist').respond(
     getJSONFixture('search.artist.json') 
    ); 

    Spotify.search('Nirvana', 'artist').then(function (data) { 
     expect(data).toBeDefined(); 
     expect(data.artists.items.length).toBeGreaterThan(0); 
    }); 

    $httpBackend.flush(); //This line causes the error 
    }); 
}); 

e l'errore che viene fuori è:

✗ should return an array of artists 
TypeError: 'undefined' is not a function (evaluating '$browser.$$checkUrlChange()') 
    at /Users/XXXX/Work/angular-spotify/bower_components/angular/angular.js:12502 
    at /Users/XXXX/Work/angular-spotify/bower_components/angular-mocks/angular-mocks.js:1438 
    at /Users/XXXX/Work/angular-spotify/test/spec/angular-spotify.spec.js:249 

linea 249 è $ httpBackend.flush()

sto usando karma-gelsomino e test di corsa attraverso PhantomJS.

  • AngularJS: 1.2.24
  • angolari-deride: 1.2.16
  • angolare scenario: 1.2.16
  • karma-gelsomino: 0.2.0

Perché sarebbe $ httpBackend sta provando a cambiare l'URL nel browser?

Qualsiasi aiuto su questo sarebbe fantastico.

risposta

7

Il problema è la mancata corrispondenza tra Angular e Angular-Mocks. Questa linea è stata aggiunta di recente in Angular-Mocks:

https://github.com/angular/angular.js/blob/v1.2.24/src/ngMock/angular-mocks.js#L59

ho potuto risolvere questo problema, spingendo sia angolare e angolare Mocks a 1.2.22 in cui questo cambiamento non è ancora presente in entrambi i progetti. Ma suppongo che 1.2.24 per entrambi funzionerebbe anche.

+0

Hai ragione haha. Era solo un missmatch di versione tra danze angolari e angolari. –

+0

ottima risposta! Grazie. – Rocco

0

Il metodo flush è parte dell'implementazione di httpBackend.

See:

https://github.com/angular/angular.js/blob/master/src/ngMock/angular-mocks.js#L1823

Per utilizzare questa implementazione di HttpBackend è necessario iniettare 'ngMockE2E' nelle vostre dipendenze.

+0

Sto iniettando angular-mocks.js nel file di configurazione karma già. Funzionava con versioni precedenti di gelsomino ecc. Ora non funziona. –

+0

Non è sufficiente, l'API è stata modificata di recente e ora per utilizzare passThrough o flush, è necessario inserire ngMockE2E. È un'iniezione di componenti e non una nuova dipendenza di file. In realtà, ngMock2E2 è parte del file mock. – benek