Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS testing $httpBackend.flush() causes error

I'm trying to do some testing with jasmine for an AngularJS service that I have been creating for Spotify. But I keep getting an error with my tests when testing promises.

My test is currently 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 comes out is:

✗ 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 is $httpBackend.flush()

I'm using karma-jasmine and running tests through PhantomJS.

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

Why would $httpBackend be trying to change the url in the browser?

Any help on this would be great.

like image 225
Ed Moore Avatar asked Sep 10 '14 07:09

Ed Moore


1 Answers

The problem is your version mismatch between Angular and Angular-Mocks. This line was added recently in 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 to 1.2.22 where this change is not present yet in both projects. But I guess 1.2.24 for both would also work.

like image 146
Andreas Hoffmann Avatar answered Sep 27 '22 22:09

Andreas Hoffmann