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
Flushing HTTP requests For this reason, the mock $httpBackend has a flush() method, which allows the test to explicitly flush pending requests. This preserves the async api of the backend, while allowing the test to execute synchronously.
HttpBackendlinkA final HttpHandler which will dispatch the request via browser HTTP APIs to a backend. abstract class HttpBackend implements HttpHandler { abstract handle(req: HttpRequest<any>): Observable<HttpEvent<any>> }
$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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With