angular - 如何在角度比较标头与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);
这给了我一个错误,显然是因为我正在比较两个不同的对象。我如何比较我的标题?谢谢
解决方案
您可以使用 HttpTestingController;
const req = httpTestingController.expectOne(sampleService.sampleMethod);
expect(req.request.method).toEqual('POST');
之后,您应该使用、刷新并验证所有请求是否已完成
req.flush(mockResponse);
httpTestingController.verify();
推荐阅读
- ios - 如何在 Swift 中分隔字符串中的两个 URL?
- python - 如何设置 python-mode 以便 linter 检查 python 3 和 python 2 错误
- vue.js - 作为道具传递的对象数组不更新子组件
- checkbox - 在 Google 表格中启用/禁用复选框
- python - TypeError:“Mul”Op 的输入“y”的类型为 float32,与参数“x”的类型 float64 不匹配
- jekyll - 如何在 GitHub Pages 上生成指向帖子原始降价文件的链接?
- azure-powershell - 获取 AzureStorageBlobContent 超时
- android - Android - 无法运行 aapt dump badging:'android:icon' 属性:属性值引用不存在
- machine-learning - Seeker 识别(机器学习)
- spring-boot - 如何使用 Spring Boot 每秒支持几千个请求