Angular 单元测试 HTTP 请求 header
Angular unit test HTTP request header
我想编写一个单元测试来检查 http 请求的 headers。但是,当我尝试访问请求 header:
时,测试总是以错误结束
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.
我尝试了各种方法,但我总是在访问 header 的“预期”行中遇到此错误。当我在控制台中记录 headers 时,它看起来像这样:
LOG: HttpHeaders{normalizedNames: Map{}, lazyUpdate: [Object{name: ..., value: ..., op: ...}], headers: Map{}, lazyInit: HttpHeaders{normalizedNames: Map{}, lazyUpdate: null, headers: Map{}}}
您的规范中是否有其他代码使用展开运算符?
请参阅下面的工作示例。
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);
});
});
我想编写一个单元测试来检查 http 请求的 headers。但是,当我尝试访问请求 header:
时,测试总是以错误结束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.
我尝试了各种方法,但我总是在访问 header 的“预期”行中遇到此错误。当我在控制台中记录 headers 时,它看起来像这样:
LOG: HttpHeaders{normalizedNames: Map{}, lazyUpdate: [Object{name: ..., value: ..., op: ...}], headers: Map{}, lazyInit: HttpHeaders{normalizedNames: Map{}, lazyUpdate: null, headers: Map{}}}
您的规范中是否有其他代码使用展开运算符?
请参阅下面的工作示例。
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);
});
});