$httpBackend 中的 expect 和 when 有什么区别

What is the difference between expect and when in $httpBackend

$httpBackend.when('')$httpBackend.expect('')有什么区别?

我不知道这两种方法有什么区别。 angularjs api 文档也对我没有帮助。

API 文档 linkhttps://docs.angularjs.org/api/ngMock/service/$httpBackend

$httpBackend.expect - 指定请求期望
$httpBackend.when - 指定后端定义

From: https://docs.angularjs.org/api/ngMock/service/$httpBackend
Request expectations provide a way to make assertions about requests made by the application and to define responses for those requests. The test will fail if the expected requests are not made or they are made in the wrong order.

Backend definitions allow you to define a fake backend for your application which doesn't assert if a particular request was made or not, it just returns a trained response if a request is made. The test will pass whether or not the request gets made during testing.

因此,这意味着如果您使用 expect 设置请求期望,如果您没有得到 完全 相同的请求,测试将失败,精确次。但是,如果您将其设置为 when,后端会做出适当的响应,但它不会预期会收到多少请求(如果有),因此不会使测试失败。