Jasmine 错误:需要一个符合条件的请求;匹配URL:xyz";,一无所获

Jasmine 错误:需要一个符合条件的请求;匹配URL:xyz";,一无所获,jasmine,angular7,Jasmine,Angular7,我现在不知所措。我正在测试一个拦截器: 测试: intercept(请求:HttpRequest,下一步:HttpHandler){ const cachedResponse=this.cache.get(req.url); console.log(cachedResponse,req.url);//返回http://localhost:3000/api/v1/employee/123 如getEmployee请求中所示 return cachedResponse?Observable.of(c

我现在不知所措。我正在测试一个拦截器:

测试:

intercept(请求:HttpRequest,下一步:HttpHandler){
const cachedResponse=this.cache.get(req.url);
console.log(cachedResponse,req.url);//返回http://localhost:3000/api/v1/employee/123 如getEmployee请求中所示
return cachedResponse?Observable.of(cachedResponse):this.sendRequest(req,next);
}
据我所知,
spyOn(cacheService,'get')。和.returnValue(mockResponse)
应该在拦截器中设置
this.cache.get
请求的响应,但它没有。我不断地得到:
失败:应为条件“匹配URL:http://localhost:3000/api/v1/employee/123“,未找到任何内容。

如果我删除了间谍,错误就会消失,但在这种情况下,我不会中断服务的响应

茉莉花3.1.0
角度7所以这里有两件事。因为我试图返回数据,而不是发送实际的HTTP请求,所以我不应该告诉httpMock期待一个请求。我应该告诉它
httpMock.expectNone(testUrl)
。其次,我与间谍一起发送的mockResponse不是订阅所期望的实际HttpResponse,我只是发送了一个对象。所以我需要做一个:

new HttpResponse({ body: employee.data.employee, status: 200 });
把间谍送回去


希望这可以节省其他人的工作时间:)

我没有看到对您的拦截代码的调用?也许HttpTestingController阻止它拦截apiService调用…@dmcgrandle-拦截器中的控制台日志确实运行,并且记录的req.url是
expectOne
调用中的url。我不确定这是否是测试拦截器的正确方法,尽管大多数教程都是这样做的。我很感谢你的帮助。这也让我大吃一惊,我在自己的测试中调用了
expectOne
intercept(req: HttpRequest<any>, next: HttpHandler) {
    const cachedResponse = this.cache.get(req.url);
    console.log(cachedResponse, req.url); // this returns the http://localhost:3000/api/v1/employee/123 as seen in the getEmployee request
    return cachedResponse ? Observable.of(cachedResponse) : this.sendRequest(req, next);
  }
new HttpResponse({ body: employee.data.employee, status: 200 });