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();
});
});