0
0
Angularframework~20 mins

Testing HTTP calls with HttpTestingController in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HttpTestingController Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Angular test using HttpTestingController?
Consider this Angular service test snippet. What will be the value of result after the test runs?
Angular
let result: any;
service.getData().subscribe(data => result = data);

const req = httpTestingController.expectOne('/api/data');
req.flush({name: 'Test'});

httpTestingController.verify();
Aresult will be {name: 'Test'}
Bresult will be undefined because flush was not called
Cresult will be null because subscribe was not triggered
DThe test will throw an error because expectOne was not called
Attempts:
2 left
💡 Hint
Remember that calling flush sends the mock response to the subscriber.
📝 Syntax
intermediate
2:00remaining
Which option correctly matches and flushes a GET request in HttpTestingController?
You want to test a GET request to '/api/items'. Which code snippet correctly matches and flushes the request?
A
const req = httpTestingController.expectOne('/api/items');
req.send([{id:1}]);
B
const req = httpTestingController.match('/api/items');
req.flush([{id:1}]);
C
const req = httpTestingController.expectOne('/api/items');
req.flush([{id:1}]);
D
const req = httpTestingController.expect('/api/items');
req.flush([{id:1}]);
Attempts:
2 left
💡 Hint
Use expectOne to match a single request and flush to send the response.
🔧 Debug
advanced
2:00remaining
Why does this Angular test using HttpTestingController fail with 'Expected one matching request for criteria' error?
Given this test code, why does httpTestingController.expectOne('/api/users') throw an error?
Angular
service.fetchUsers().subscribe();
// No HTTP call is made inside fetchUsers
httpTestingController.expectOne('/api/users');
ABecause no HTTP request to '/api/users' was made, so expectOne fails to find it
BBecause the URL '/api/users' is incorrect and should be '/api/user'
CBecause the flush method was not called before expectOne
DBecause the subscribe callback is empty and does not trigger the request
Attempts:
2 left
💡 Hint
Check if the service method actually makes an HTTP call.
state_output
advanced
2:00remaining
What is the value of errorResponse after this test triggers an HTTP error?
In this Angular test, what will errorResponse contain after the HTTP call fails?
Angular
let errorResponse: any;
service.getData().subscribe({
  next: () => {},
  error: err => errorResponse = err.status
});

const req = httpTestingController.expectOne('/api/data');
req.flush('Not Found', {status: 404, statusText: 'Not Found'});

httpTestingController.verify();
A'Not Found'
B404
Cundefined
Dnull
Attempts:
2 left
💡 Hint
The error callback receives an HttpErrorResponse object with a status property.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of HttpTestingController in Angular tests?
Choose the most accurate description of what HttpTestingController does in Angular unit tests.
AIt records HTTP requests and replays them later during integration tests
BIt automatically mocks all HTTP services without needing explicit request matching
CIt replaces HttpClient with a fake implementation that returns static data only
DIt intercepts HTTP requests and allows tests to assert and respond to them without real network calls
Attempts:
2 left
💡 Hint
Think about how HttpTestingController helps control HTTP calls in tests.