【发布时间】:2014-09-10 16:25:15
【问题描述】:
我正在尝试使用 jasmine 对我为 Spotify 创建的 AngularJS 服务进行一些测试。但是在测试 Promise 时,我的测试总是出错。
我的测试目前是这样的:
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
});
});
而出现的错误是:
✗ 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
第 249 行是 $httpBackend.flush()
我正在使用 karma-jasmine 并通过 PhantomJS 运行测试。
- AngularJS:1.2.24
- 角度模拟:1.2.16
- 角度场景:1.2.16
- 业力茉莉花:0.2.0
为什么 $httpBackend 会尝试更改浏览器中的 url?
在这方面的任何帮助都会很棒。
【问题讨论】:
标签: javascript angularjs testing karma-jasmine