Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between expect and when in $httpBackend

Tags:

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

like image 292
Jiangfei Avatar asked Jan 14 '15 14:01

Jiangfei


People also ask

What does HttpBackend flush do?

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.

What is HttpBackend in angular?

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>> }


1 Answers

$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.

like image 157
Umur Kontacı Avatar answered Oct 12 '22 09:10

Umur Kontacı