angularjs – 如何验证根本没有发出http请求?
发布时间:2020-12-17 09:18:01 所属栏目:安全 来源:网络整理
导读:如何验证没有调用任何http请求方法来执行任何请求.我有这个代码: $scope.getSubnetsPageDetails = function (pageNumber) { $http.get(URLS.subnetsPagesCount(pageNumber)).success(function (response) { $scope.pageDetails = response; }).error(functi
如何验证没有调用任何http请求方法来执行任何请求.我有这个代码:
$scope.getSubnetsPageDetails = function (pageNumber) { $http.get(URLS.subnetsPagesCount(pageNumber)).success(function (response) { $scope.pageDetails = response; }).error(function (response,errorCode) { }); }; 而这个测试: it("should not allow request with negative page number",function () { scope.getSubnetsPageDetails(-1); //verify that htt.get is not invoked at all }); 如何验证未调用http.get?
您可以使用
$httpBackend mock中的verifyNoOutstandingRequest()方法测试没有进行调用.
通常这些验证是在Jasmine测试的afterEach部分完成的.除此之外,通常调用另一个方法verifyNoOutstandingExpectation()来验证是否实际调用了所有预期的调用. 这是代码,你需要注入$httpBackend mock: var $httpBackend; beforeEach(inject(function($injector) { $httpBackend = $injector.get('$httpBackend'); })); 然后你测试,最后: afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); }); 当然,您可以在单个测试中调用$httpBackend.verifyNoOutstandingRequest().上述http://docs.angularjs.org/api/ngMock.$httpBackend页面提供了有关该主题的丰富信息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |