$httpBackend.expect vs. $httpBackend.when

After reading the AngularJS document fo $httpBackend, I am still not very clear what the difference between $httpBackend.expect and $httpBackend.when as both are functions for mocking http requests.



Could someone help explain the difference between these two functions? And also when shall I use $httpBackend.when instead of $httpBackend.expect?



Answers

The answer is fairly clear in the documentation, however, here is the main difference:



$httpBackend.when will return you a fake http response, but not fail the test if the URL declared is never called.



$httpBackend.expect will return you a fake http response. but fail the test if the URL declared is never called.



So, expect sets up an test expectation that the test must call the URL that is set up.