我正在使用截获 401 响应的angular-http-auth模块。event:auth-loginRequired
如果有可以使用 $on() 接收的 401 响应,此模块会广播。但是我该如何测试呢?
beforeEach(inject(function($injector, $rootScope) {
$httpBackend = $injector.get('$httpBackend');
myApi = $injector.get('myApi');
scope = $rootScope.$new();
spyOn($scope, '$on').andCallThrough();
}));
describe('API Client Test', function() {
it('should return 401', function() {
$httpBackend.when('GET', myApi.config.apiRoot + '/user').respond(401, '');
myApi.get(function(error, success) {
// this never gets triggered as 401 are intercepted
});
scope.$on('event:auth-loginRequired', function() {
// This works!
console.log('fired');
});
// This doesn't work
expect($scope.$on).toHaveBeenCalledWith('event:auth-loginRequired', jasmine.any(Function));
$httpBackend.flush();
});
});