小编Ed *_*ore的帖子

AngularJS测试$ httpBackend.flush()会导致错误

我正在尝试使用jasmine对我为Spotify创建的AngularJS服务进行一些测试.但在测试承诺时,我的测试一直出错.

我的测试目前是这样的:

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
  });
});
Run Code Online (Sandbox Code Playgroud)

而出现的错误是:

? 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 …
Run Code Online (Sandbox Code Playgroud)

javascript testing angularjs karma-jasmine

5
推荐指数
1
解决办法
4736
查看次数

标签 统计

angularjs ×1

javascript ×1

karma-jasmine ×1

testing ×1