angular - 错误:预期有一个匹配条件的请求...没有找到
问题描述
我已经看到了很多关于这个错误的问题,但是今天大部分时间都在尝试每个解决方案,我需要你的帮助。
我正在尝试测试这个服务,它是一个 http get 请求
getTrainInformation(url: string): Observable<any> {
return this.http.get(url,
{
params:
{
expand: 'true'
}
}
).pipe(catchError(this.handleError));
}
这是我的测试:
import { HttpTestingController, HttpClientTestingModule } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { TrainInformationService } from "./traininformation.service";
describe('traininformation service', () => {
let service: TrainInformationService;
let httpTestingController: HttpTestingController;
beforeEach( () => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [TrainInformationService]
});
service = TestBed.get(TrainInformationService);
httpTestingController = TestBed.get(HttpTestingController);
});
it('should perform a GET', () => {
const url = service.getFullUrl('BOP');
const urlParam = { param: 'expand', value: 'true'};
const urlWithParams = `${url}?${urlParam.param}=${urlParam.value}`;
service.getTrainInformation(urlWithParams).subscribe();
const req = httpTestingController.expectOne(urlWithParams);
console.log('req is ' + req);
req.flush('Some');
// httpTestingController.verify();
});
});
当测试运行时,它失败了:
错误:应为条件“匹配 URL:<<the_expected_url>>,找到一个匹配请求。
如果我更改测试以便注释掉对 expectOne 的调用并刷新并调用 verify,那么测试变为:
it('should perform a GET', () => {
const url = service.getFullUrl('BOP');
const urlParam = { param: 'expand', value: 'true'};
const urlWithParams = `${url}?${urlParam.param}=${urlParam.value}`;
service.getTrainInformation(urlWithParams).subscribe();
// const req = httpTestingController.expectOne(urlWithParams);
// console.log('req is ' + req);
// req.flush('Some');
httpTestingController.verify();
});
我现在看到的错误是:
错误:预期没有打开的请求,发现 1:GET <<the_expected_url>>
我究竟做错了什么?
解决方案
我不认为你做错了什么。我遇到了和你一样的问题,我无法处理请求,这很可能是因为请求有参数,尽管 URL 似乎是一对一的,但我们无法通过参数匹配它。如果没有参数,我可以轻松处理请求。
为了使用参数处理请求,我使用了不同的签名来httpTestingController.expectOne
传递具有匹配条件的对象。
afterEach(() => {
httpTestingController.verify();
});
it('should perform a GET', () => {
const url = service.getFullUrl('BOP');
const urlParam = { param: 'expand', value: 'true'};
const urlWithParams = `${url}?${urlParam.param}=${urlParam.value}`;
service.getTrainInformation(urlWithParams).subscribe(response => {
expect(response).toBe('Some'); // put an assertion here (optional).
});
const req = httpTestingController.expectOne({
method: 'GET', // find open Http request that is a get request.
});
console.log('req is ' + req); // should have a handle on it now
// below asserts the params
expect(req.request.params.toString()).toBe('${urlParam.param}=${urlParam.value}');
req.flush('Some');
});
推荐阅读
- javascript - 悬停时跟随指针的 Div
- python - 即使在重塑后,sklearn 也无法将我的数组识别为 2d?
- debugging - 无法从存储“C:\Users\aleja\.android\debug.keystore”读取密钥 AndroidDebugKey:检测过早的 EOF
- swift - 分割 UITableView 单元格 - 重复单元格
- swift - UIScrollView Delegate 作为协议默认实现
- sql - 来自错误位置的关键字
- python - 如何在 Python - SQL 中修复“表”对象没有属性
- mysql - 如何使用 prometheus 和 grafana 监控多个 mysql?
- angular - Firebase 数据库自动生成神秘节点子对象迭代
- r - 使用 RSelenium 找不到并单击动态元素