【问题标题】:Angular unit test HTTP request headerAngular 单元测试 HTTP 请求标头
【发布时间】:2021-12-19 12:28:21
【问题描述】:

我想编写一个单元测试来检查 http 请求的标头。但是,当我尝试访问请求标头时,测试总是以错误结束:

it('should have my header', () => {    
  httpClient.get('/someURL').subscribe();
  const req = httpTestingController.match({ method: 'get' });
  console.log(req[0].request.headers);   
  expect(req[0].request.headers.has('Custom-Header')).toEqual(true);
  httpTestingController.verify();
});

单元测试失败并出现以下错误:

TypeError: Invalid attempt to spread non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.

我尝试了各种方法,但我总是在访问标题的“预期”行中收到此错误。当我在控制台中记录标题时,它看起来像这样:

LOG: HttpHeaders{normalizedNames: Map{}, lazyUpdate: [Object{name: ..., value: ..., op: ...}], headers: Map{}, lazyInit: HttpHeaders{normalizedNames: Map{}, lazyUpdate: null, headers: Map{}}}

【问题讨论】:

    标签: angular karma-jasmine angular-test


    【解决方案1】:

    您的规范中是否还有其他使用扩展运算符的代码?

    请参阅下面的工作示例。

    import { HttpClient, HttpClientModule, HttpHeaders, HttpRequest } from '@angular/common/http';
    import {
      HttpClientTestingModule,
      HttpTestingController,
      TestRequest,
    } from '@angular/common/http/testing';
    import { TestBed, waitForAsync } from '@angular/core/testing';
    
    describe('headers test', () => {
      let httpTestingController: HttpTestingController;
      let httpClient: HttpClient;
    
      beforeEach(
        waitForAsync(() => {
          void TestBed.configureTestingModule({
            imports: [HttpClientTestingModule, HttpClientModule],
          })
            .compileComponents()
            .then(() => {
              httpTestingController = TestBed.inject(HttpTestingController);
              httpClient = TestBed.inject(HttpClient);
            });
        }),
      );
    
      afterEach(() => {
        httpTestingController
          .match((req: HttpRequest<unknown>): boolean => true)
          .forEach((req: TestRequest) => (!req.cancelled ? req.flush({}) : null));
        httpTestingController.verify();
      });
    
      it('should have my header', () => {
        const headers = new HttpHeaders().set('Custom-Header', 'test');
        void httpClient.get('/someURL', { headers }).subscribe();
        const req = httpTestingController.match('/someURL');
        console.warn('req', req);
        console.warn(req[0].request.headers);
        expect(req[0].request.headers.has('Custom-Header')).toEqual(true);
      });
    });
    
    

    【讨论】:

    • 您的代码也可以正常工作。但我的也是。问题是我在测试中使用的一个模拟类。
    猜你喜欢
    • 2017-11-23
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2017-10-07
    • 2015-07-13
    相关资源
    最近更新 更多