【问题标题】:$httpBackend jsonp: No pending requests to flush$httpBackend jsonp:没有待刷新的请求
【发布时间】:2015-11-16 03:48:38
【问题描述】:

单击按钮时,我会进行 jsonp 调用。

    function clickHandler(e) {
            e.preventDefault();
            var url = "some.url.with?params";
            $http.jsonp(url).success(function () {$scope.success();})
                            .error(function () {$scope.failure();});
        }

测试:

$httpBackend.expectJSONP(this.url + '&' + $.param(this.params)).respond({status: 200});

$('button').click();

$rootScope.$digest(); // this was suggested in few answers, doesn't work for me though

$httpBackend.flush();

但我不断收到 No pending requests to flush failure

在 JSONP 调用的情况下我们需要做一些不同的事情吗?这种格式在其他任何地方都有效。

PS:是的,我打了电话(很多问题,人们实际上都在打电话或触发触发事件的操作)。至少,我看到代码到达了我在代码中提出请求的那一行。

【问题讨论】:

  • clickHandler() 在控制器中吗?它是如何绑定到按钮的? ng-click?
  • 是的,它在控制器内并通过 ng-click 绑定
  • 我认为在您的测试中获取控制器实例会更好,然后您可以调用controller.clickHandler() 而不是$('button').click()。如果您绝对必须使用$('button').click(),您可以尝试将$rootScope.$digest() 更改为$rootScope.$apply,但我认为这不会有帮助。
  • 你不会相信我都试过了。它工作正常。我试图从指令的范围内直接访问该方法,不起作用。

标签: javascript angularjs unit-testing jsonp httpbackend


【解决方案1】:

我的测试受到套件中早期测试的影响,因此从早期测试中提取的值与我所断言的不同。因此请求不匹配。纠正了他们。现在工作。给您添麻烦了。

【讨论】:

    【解决方案2】:

    这不起作用,因为您似乎没有在测试中创建 button DOM 元素。如果您只需在 $scope 对象上调用该函数,您的生活就会变得更轻松,因为您已经创建了它,因此您可以在测试中轻松访问该函数。试试这个:

    DEMO

    控制器

    var app = angular.module('plunker', []);

    app.controller('MainCtrl', function($scope, $http) {
    
      $scope.success = function(){
        console.log('success');
      };
    
      $scope.failure = function(){
        console.log('failure');
      };
    
      $scope.clickHandler = function(e) {
          e.preventDefault();
          var url = "some.url.with?params";
          $http
            .jsonp(url)
            .success($scope.success)
            .error($scope.failure);
      }
    
    });
    

    规格

    describe('Testing a controller', function() {
    
      var $scope, ctrl, $httpBackend, mockEvent;
    
      beforeEach(module('plunker'));
    
      beforeEach(inject(function($rootScope, $controller, _$httpBackend_){
    
        $scope        = $rootScope.$new();
        $httpBackend  = _$httpBackend_;
    
        mockEvent = { preventDefault: function(){} };
    
        ctrl = $controller('MainCtrl', {
          $scope: $scope
        });
    
      })); 
    
      it('should trigger success callback', function() {
    
        spyOn($scope, 'success');
    
        $httpBackend.expectJSONP('some.url.with?params').respond({status: 200});
    
        $scope.clickHandler(mockEvent);
    
        $scope.$digest();
    
        $httpBackend.flush();
    
        expect($scope.success).toHaveBeenCalled();
    
      });
    
    });
    

    【讨论】:

    • 就像我在前面的评论中所说的那样,它也不起作用。通过角度代码“Unexpected JSONP”进行调试后,我注意到一个错误。如果我使用 url 发出实际请求,它工作正常。这就是为什么我问“在 JSONP 调用的情况下我们需要做些什么不同的事情”
    • 是否也应该将响应包装在伪造响应中的方法中?
    • 按钮点击不是问题......仅供参考:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多