테스트 벨로우즈가 1을 반환해야하므로 $rootScope.$broadcast.reset();
이 제대로 작동하지 않는 것으로 보입니다. 그러나 각 테스트 케이스마다 $broadcast
을 재설정하기 위해 코드를 사용하고 있습니다. 그것은 6.andCallThrough()를 사용하는 동안 각 테스트 케이스마다 브로드 캐스트() 재설정
전에 내가 andCallThrough()
기능없이 사용하기 때문에 이것에 대한 이유는, andCallThrough()
것 같다 반환하지만 일부 리팩터링 후에는 TypeError: Cannot read property 'defaultPrevented' of undefined
것을 나에게 오류를 준, 그래서 그 오류를 방지하기 위해 사용했다.
질문은 andCallThrough
을 사용하는 동안 broadcast
을 어떻게 재설정 할 수 있습니까? 아니면 더 정확한 접근 방법이 있습니까?
beforeEach(function() {
spyOn($http, 'post');
spyOn($rootScope, '$broadcast').andCallThrough();
});
afterEach(function() {
$http.post.reset();
$rootScope.$broadcast.reset();
});
it('should make a POST request to API endpoint', function() {
$http.post.andCallThrough();
var response = { id: '123', role: 'employee', email: '[email protected]', username: 'someUsername' };
$httpBackend.expectPOST(apiUrl + 'login').respond(response);
service.login();
$httpBackend.flush();
$timeout.flush();
expect($rootScope.$broadcast.callCount).toBe(1);
expect($rootScope.$broadcast).toHaveBeenCalledWith(AUTH_EVENTS.loginSuccess, response);
});
'callCount'를 테스트하는 'expect'호출이 테스트의 첫 번째 줄로 이동되면,'callCount'는 그럼? 테스트에서 뭔가 다른 것이'$ broadcast'를 호출하는지 궁금합니다. –