简体   繁体   English

无法使用$ httpBackend测试POST $ http请求

[英]Can't test POST $http request with $httpBackend

Trying to cover http requests of my app by unit tests. 尝试通过单元测试覆盖我的应用程序的http请求。 I use Jasmine and Karma and step by step write my code using this tutorial: https://docs.angularjs.org/api/ngMock/service/ $httpBackend. 我使用Jasmine和Karma,并使用本教程逐步编写代码: https ://docs.angularjs.org/api/ngMock/service/ $ httpBackend。 But I get an error: 但是我得到一个错误:

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)

This answer Unit testing AngularJS controller with $httpBackend is so popular for my problem but when I move const controller = createController(); 对于我的问题, 使用$ httpBackend进行单元测试AngularJS控制器是如此受欢迎,但是当我移动const controller = createController(); down - I've got another one error: Error: Unsatisfied requests: POST http://loalhost:5000/register . Error: Unsatisfied requests: POST http://loalhost:5000/register我又遇到一个错误: Error: Unsatisfied requests: POST http://loalhost:5000/register

Where is my mistake? 我的错误在哪里? What I implemented wrong? 我实施错了什么?

My unit test: 我的单元测试:

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();
});

Block of code from the controller: 来自控制器的代码块:

  $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 UPD

I also tried to add before each $httpBackend.flush() this one (like here Angularjs testing (Jasmine) - $http returning 'No pending request to flush' ): 我还尝试在每个$httpBackend.flush()之前添加一个(例如这里的Angularjs测试(茉莉花)-$ http返回“无刷新请求” ):

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

But anyway, nothing change. 但是无论如何,什么都没有改变。

You call $httpBackend.flush() after controller creation here: 在创建控制器后,您可以在此处调用$ 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();
});

Should be like this: 应该是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM