24

$httpBackend.when('')和 和有什么不一样$httpBackend.expect('')

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

API 文档链接https : //docs.angularjs.org/api/ngMock/service/ $httpBackend

4

1 回答 1

25

$httpBackend.expect - specifies a request expectation
$httpBackend.when - specifies a backend definition

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.

Therefore, it means that if you set a request expectation with expect the test will fail if you don't get the exact same request, exact number of times. However if you set it with when, the backend will respond appropriately, but it has no expectations about how many requests (if any) will come therefore will not fail the test.

于 2015-01-14T15:00:38.880 回答