0
0
Angularframework~5 mins

Testing HTTP calls with HttpTestingController in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is HttpTestingController used for in Angular testing?

HttpTestingController lets you mock and verify HTTP requests in Angular tests without making real network calls.

Click to reveal answer
beginner
How do you inject HttpTestingController in an Angular test?

Use Angular's TestBed.inject(HttpTestingController) inside your test setup to get the controller instance.

Click to reveal answer
beginner
What method do you use to expect a specific HTTP request in your test?

Use httpTestingController.expectOne(urlOrPredicate) to find and assert a single HTTP request matching the URL or condition.

Click to reveal answer
intermediate
How do you simulate a response to an HTTP request in tests?

Call req.flush(data) on the matched request to send back mock data as the HTTP response.

Click to reveal answer
intermediate
Why should you call httpTestingController.verify() at the end of your test?

It checks that no unexpected HTTP requests are left outstanding, ensuring your test covers all HTTP calls.

Click to reveal answer
Which Angular service helps you mock HTTP requests in tests?
AHttpClient
BHttpClientModule
CHttpTestingController
DHttpInterceptor
What does expectOne() do in HttpTestingController?
AMocks multiple HTTP requests
BFinds exactly one matching HTTP request
CCancels all HTTP requests
DSends a real HTTP request
How do you simulate a server response in an Angular HTTP test?
AUse <code>subscribe()</code> on HttpTestingController
BCall <code>send()</code> on HttpClient
CCall <code>verify()</code> on HttpClient
DCall <code>flush()</code> on the matched request
Why is httpTestingController.verify() important?
AIt ensures no unexpected HTTP requests remain
BIt sends all pending HTTP requests
CIt resets the HTTP client
DIt logs HTTP requests to console
Where do you usually inject HttpTestingController in Angular tests?
AInside the test setup using <code>TestBed.inject()</code>
BIn the component constructor
CIn the service constructor
DIn the main app module
Explain how to test an Angular service's HTTP GET call using HttpTestingController.
Think about the steps to mock and verify the HTTP call in a test.
You got /5 concepts.
    Describe why mocking HTTP calls is better than making real HTTP requests in Angular tests.
    Consider what makes tests stable and fast.
    You got /5 concepts.