Angular的
$httpBackend service允许您期望带有expectGET,expectPOST等的HTTP请求(或者只是期望).
我怎么写一个测试说“控制器不应该向这个端点发出请求(在这些条件下)”?
我想的是:
$httpBackend.when('/forbidden/endpoint').respond(function() { throw Error("Shouldn't be making a request to /forbidden/endpoint!"); });
这对我来说似乎有些苛刻,但如果这是正常的做事方式,我对它很好. (但我对此表示怀疑.)
我偶然发现了同样的问题.
原文链接:https://www.f2er.com/angularjs/141568.html解决方案是将回调函数作为响应,在内部你可以期待(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); });