测试 HttpInterceptor Angular 10 时出错

Cam*_*ron 5 testing typescript angular angular-httpclient angular-httpclient-interceptors

我正在开发一个 Angular 10 应用程序,该应用程序使用HttpInterceptor向所有响应添加特定标头。不幸的是,在尝试测试此拦截器时,我不断收到以下错误:

Error: Expected one matching request for criteria "Match by function: ", found none.
Run Code Online (Sandbox Code Playgroud)

或类似的变体:

Error: Expected one matching request for criteria "Match URL: /endpoint", found none.
Run Code Online (Sandbox Code Playgroud)

我的期望是这个测试会通过,但我现在不知道为什么它不起作用。

这是我的拦截器:

Error: Expected one matching request for criteria "Match by function: ", found none.
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

Error: Expected one matching request for criteria "Match URL: /endpoint", found none.
Run Code Online (Sandbox Code Playgroud)

也就是说,可能值得指出的是,我尝试应用以下资源中的解决方案但无济于事(大多数似乎与我已经拥有的类似,而且许多似乎适用于旧版本的 Angular):

  • 来自 Angular 4 的单元测试 HttpInterceptor:在这里,我发现了一篇有用的文章,它几乎完全满足了我的需求,但实际上并不适用于我的情况(我遇到了上述错误)。我这么说几乎完全是因为我用来获取令牌的服务实际上并没有使用HttpClient我所知道的。
  • 并不完全像我的问题,但它足够相似,可以尝试一下。我尝试使用教科书描述的解决方法,但显然这与我遇到的问题不够接近。
  • 我也尝试spyOnhttp.get(...)在我的测试功能,导致被淘汰上面的错误,但它并没有叫我的intercept功能时,我这样做。

非常感谢任何帮助 - 谢谢!