angularjs – 我怎么期望不发出HTTP请求?
发布时间:2020-12-17 07:18:24 所属栏目:安全 来源:网络整理
导读:Angular的 $httpBackend service允许您期望带有expectGET,expectPOST等的HTTP请求(或者只是期望). 我怎么写一个测试说“控制器不应该向这个端点发出请求(在这些条件下)”? 我想的是: $httpBackend.when('/forbidden/endpoint').respond(function() { throw
Angular的
$httpBackend service允许您期望带有expectGET,expectPOST等的HTTP请求(或者只是期望).
我怎么写一个测试说“控制器不应该向这个端点发出请求(在这些条件下)”? 我想的是: $httpBackend.when('/forbidden/endpoint').respond(function() { throw Error("Shouldn't be making a request to /forbidden/endpoint!"); }); 这对我来说似乎有些苛刻,但如果这是正常的做事方式,我对它很好. (但我对此表示怀疑.)
我偶然发现了同样的问题.
解决方案是将回调函数作为响应,在内部你可以期待(true).toBe(false)或者在我看来更美丽的东西: it ('should not trigger HTTP request',function() { var forbiddenCallTriggered = false; $httpBackend .when('/forbidden/endpoint') .respond(function() { forbiddenCallTriggered = true; return [400,'']; }); // do whatever you need to call. $rootScope.$digest(); $httpBackend.flush(); // Let test fail when request was triggered. expect(forbiddenCallTriggered).toBe(false); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读