首页 > 解决方案 > 如何在角度比较标头与httpclient间谍

问题描述

我需要检查是否使用我在 it() 测试中设置的相同标头调用我的发布请求。

我正在做的是:

let jwt = 'aaa.bbb.ccc';
let jsonRequest: any = {};
let headers = {headers: new HttpHeaders({'Authorization': 'Bearer ' + String(jwt)})}

在我的 post.subscribe 中:

  expect(httpClientSpy.post).toHaveBeenCalledWith(theAppURL + '?verb', jsonRequest, headers);

这给了我一个错误,显然是因为我正在比较两个不同的对象。我如何比较我的标题?谢谢

标签: angularhttpclientkarma-jasmineangular-test

解决方案


您可以使用 HttpTestingController;

const req = httpTestingController.expectOne(sampleService.sampleMethod);
expect(req.request.method).toEqual('POST');

之后,您应该使用、刷新并验证所有请求是否已完成

req.flush(mockResponse);
httpTestingController.verify();

推荐阅读