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.
Why would $httpBackend be trying to change the url in the browser?
Any help on this would be great.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With