大佬教程收集整理的这篇文章主要介绍了angularjs – 在使用andCallThrough()时在每个测试用例后重置broadcast(),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
似乎这个的原因是和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); });
以上是大佬教程为你收集整理的angularjs – 在使用andCallThrough()时在每个测试用例后重置broadcast()全部内容,希望文章能够帮你解决angularjs – 在使用andCallThrough()时在每个测试用例后重置broadcast()所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。