Angular testing $ httpBackend.flush () throws error

I am trying to do some jasmine testing for the AngularJS service that I created for Spotify. But when testing promises, I always get error messages.

My test currently looks like this:

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 }); }); 

and the error that appears:

 โœ— 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 

Line 249 - $ httpBackend.flush ()

I use karma jasmine and run tests through PhantomJS.

  • AngularJS: 1.2.24
  • angular -mocks: 1.2.16
  • angular -scenario: 1.2.16
  • karma jasmine: 0.2.0

Why is $ httpBackend trying to change the URL in the browser?

Any help on this would be great.

+5
source share
2 answers

The problem is your version mismatch between Angular and Angular -Mocks. This line has recently been added to Angular-Mocks:

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

I could fix this by pushing both Angular and Angular -Mocks before 1.2.22, where this change is not yet present in both projects. But I think that 1.2.24 will work for both too.

+7
source

The flush method is part of the ridiculed httpBackend implementation.

See:

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

To use this implementation of HttpBackend, you need to enter "ngMockE2E" in your dependencies.

0
source

Source: https://habr.com/ru/post/1202217/


All Articles