我在每个测试用例后使用代码bellow重置$broadcast,但它似乎是$rootScope.$broadcast.reset();不能正常工作,因为测试波纹管应该返回1,但它返回6.
似乎这个的原因是和CallThrough(),因为在我使用它之前没有使用andCallThrough()函数,但是在一些重构之后它给了我一个错误,即TypeError:无法读取属性’defaultPrevented’的undefined,所以我不得不用来防止那个错误.
问题是如何在使用andCallThrough时重置广播,还是有另一种更精确的方法?
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: 'user@email.com',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); });
解决方法
经过长时间调查在这种情况下的工作原理,最后通过测试,解决方案是当前的:
问题不是关于重置broadcast()或在使用andCallThrough()之后在每个测试用例之后调用reset方法.
问题是$rootScope.$broadcast.andCallThrough();由其他事件触发,而.callCount()函数返回6,这基本上意味着$broadcast spy被调用了6次.在我的情况下,我只对AUTH_EVENTS.loginSuccess事件感兴趣,并确保它只被广播一次.
expect($rootScope.$broadcast.callCount).toBe(1); expect($rootScope.$broadcast).toHaveBeenCalledWith(AUTH_EVENTS.loginSuccess,response);
因此挖掘$rootScope.$broadcast.calls的方法给了我所有调用的数组,上面的两个调用应该从中检索.因此,解决方案是:
it('should make a POST request to API endpoint',function() { $http.post.andCallThrough(); var response = { id: '123',username: 'someUsername' }; $httpBackend.expectPOST(apiUrl + 'login').respond(response); service.login(); $httpBackend.flush(); $timeout.flush(); var loginSuccessTriggerCount = _($rootScope.$broadcast.calls) .chain() .map(function getFirstArgument(call) { return call.args[0]; }) .filter(function onlyLoginSuccess(eventName) { return eventName === AUTH_EVENTS.loginSuccess; }) .value().length; expect(loginSuccessTriggerCount).toBe(1); });