angular-resource – 多个连续的httpBackend请求导致意外请求
发布时间:2020-12-17 17:56:16 所属栏目:安全 来源:网络整理
导读:我正在测试轮询资源的序列,直到满足条件. Book = $resource("/books/:id",{id: "@id"});function poll(success) { Book.get({id:1},function() { if (canStop) { success(); } else { $timeout(poll,1000); } });}; 下面的测试失败,错误:不满意的请求:GET
我正在测试轮询资源的序列,直到满足条件.
Book = $resource("/books/:id",{id: "@id"}); function poll(success) { Book.get({id:1},function() { if (canStop) { success(); } else { $timeout(poll,1000); } }); }; 下面的测试失败,错误:不满意的请求:GET / user_workshops / 1 describe('poll',function() { beforeEach(function() { $httpBackend.expectGET('/books/1').respond(200,{id:1}); $httpBackend.expectGET('/books/1').respond(200,{id:1,newVal:1}); poll(function() { successCalled = true; }); $httpBackend.flush(); $timeout.flush(); canStop=true; $httpBackend.flush(); }); it('should call success when canStop is true',function() { expect(successCalled).toBe(true); }); }); 我已经尝试重新排列测试顺序,将第二个expectGET放在第二个httpBackend.flush()之前,但后来我得到: Error: Unexpected request: POST /books/1 No more request expected 解决方法
经过一个小时的拔毛后,我意识到httpBackend对于测试被调用的顺序是非常具体的 – 期望必须不是在调用flush之前设置,而是在发出资源请求之前设置,当你调用flush时你必须已经完全,只有预期的要求.
这意味着如果要在顺序请求之间进行刷新,请求和期望的顺序必须完全符合: $httpBackend.expectGET('...') resource.get(); $httpBackend.flush() $httpBackend.expectGET('...') resource.get(); $httpBackend.flush() ... etc 因此,对于上面的代码,如果我将排序更改为: describe('poll',{id:1}); poll(function() { successCalled = true; }); $httpBackend.flush(); $httpBackend.expectGET('/books/1').respond(200,newVal:1}); $timeout.flush(); canStop=true; $httpBackend.flush(); }); it('should call success when canStop is true',function() { expect(successCalled).toBe(true); }); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读