angularjs – 在使用andCallThrough()时在每个测试用例后重置bro
发布时间:2020-12-17 17:40:59 所属栏目:安全 来源:网络整理
导读:我在每个测试用例后使用代码bellow重置$broadcast,但它似乎是$rootScope.$broadcast.reset();不能正常工作,因为测试波纹管应该返回1,但它返回6. 似乎这个的原因是和CallThrough(),因为在我使用它之前没有使用andCallThrough()函数,但是在一些重构之后它给了
我在每个测试用例后使用代码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方法. 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); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |