问题
What is the difference between $httpBackend.when('')
and $httpBackend.expect('')
?
I don't know the difference between these two methods. Also the angularjs api doc does not help me.
API documentation link: https://docs.angularjs.org/api/ngMock/service/$httpBackend
回答1:
$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.
来源:https://stackoverflow.com/questions/27945758/what-is-the-difference-between-expect-and-when-in-httpbackend