【问题标题】:Can't test POST $http request with $httpBackend无法使用 $httpBackend 测试 POST $http 请求
【发布时间】:2017-06-17 16:58:31
【问题描述】:

试图通过单元测试覆盖我的应用程序的 http 请求。我使用 Jasmine 和 Karma 并使用本教程逐步编写我的代码:https://docs.angularjs.org/api/ngMock/service/$httpBackend。但我得到一个错误:

Error: No pending request to flush !
        at Function.$httpBackend.flush (eval at <anonymous> (app/index.js:105:1), <anonymous>:1839:41)
        at Object.eval (eval at <anonymous> (app/index.js:137:1), <anonymous>:37:20)

这个答案Unit testing AngularJS controller with $httpBackend 对我的问题非常受欢迎,但是当我将const controller = createController(); 向下移动时,我又遇到了一个错误:Error: Unsatisfied requests: POST http://loalhost:5000/register

我的错误在哪里?我执行错了什么?

我的单元测试:

beforeEach(window.module(ngModule.name));

let $httpBackend, $rootScope, $controller, $scope, createController;

beforeEach(
  inject($injector => {
    $httpBackend = $injector.get('$httpBackend');
    $controller = $injector.get('$controller');
    $rootScope = $injector.get('$rootScope');
    $scope = $rootScope.$new();

    createController = () =>
      $controller('RegistrationController', { $scope });
  })
);

afterEach(() => {
  $httpBackend.verifyNoOutstandingExpectation();
  $httpBackend.verifyNoOutstandingRequest();
});

it('should successfully register user', () => {
  const controller = createController();
  $httpBackend.flush();
  $httpBackend.expectPOST('http://localhost:5000/register').respond();
  $scope.next('hello@gmail.com', '123456');
  $httpBackend.flush();
});

来自控制器的代码块:

  $scope.status;
  $scope.isFetching = false;

  const handleResponse = response => {
    $scope.status = response.status;
    $scope.isFetching = false;
  };

  $scope.next = (email, password) => {
    $scope.isFetching = true;
    $http
      .post('http://localhost:5000/register', { email, password })
      .then(response => handleResponse(response))
      .catch(response => handleResponse(response));
  };

UPD

我也尝试在每个$httpBackend.flush()之前添加这个(比如这里Angularjs testing (Jasmine) - $http returning 'No pending request to flush'):

if(!$rootScope.$$phase) {
    $rootScope.$apply();
}

但无论如何,什么都没有改变。

【问题讨论】:

    标签: javascript angularjs jasmine karma-runner karma-jasmine


    【解决方案1】:

    您在此处创建控制器后调用 $httpBackend.flush():

    it('should successfully register user', () => {
      const controller = createController();
      $httpBackend.flush(); //<-- this is not necessary if controller creation doesn't involve $http calls
      $httpBackend.expectPOST('http://loalhost:5000/register').respond();
      $scope.next('hello@gmail.com', '123456');
      $httpBackend.flush();
    });
    

    应该是这样的:

    it('should successfully register user', () => {
      const controller = createController();
      $httpBackend.expectPOST('http://loalhost:5000/register').respond();
      $scope.next('hello@gmail.com', '123456');
      $httpBackend.flush();
    });
    

    【讨论】:

    • 天哪。我花了大约 2 个小时来解决这个问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 2021-03-31
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2016-11-02
    相关资源
    最近更新 更多