A. 'GET' if the tested service made a GET request to '/api/data'
B. 'POST' regardless of the actual request method
C. Throws an error because request is undefined
D. 'PUT' if the tested service made a PUT request to '/api/data'
Solution
Step 1: Understand expectOne returns a TestRequest
TestRequest has a request property with HTTP method info.
Step 2: The method reflects the actual HTTP call
If the tested service called GET on '/api/data', req.request.method is 'GET'.
Final Answer:
'GET' if the tested service made a GET request to '/api/data' -> Option A
Quick Check:
req.request.method matches actual HTTP method = A [OK]
Hint: expectOne().request.method shows actual HTTP method used [OK]
Common Mistakes:
Assuming method is always POST or PUT
Thinking request property is undefined
Confusing expectOne with expectNone
4. What is the likely cause of this error in an Angular HTTP test?
Error: Expected one matching request for criteria "Match URL: '/api/items'", found none.
medium
A. The test forgot to call httpMock.verify()
B. The tested service did not make any HTTP request to '/api/items'
C. HttpTestingController was not injected properly
D. The URL in expectOne has a typo but the request was made correctly
Solution
Step 1: Analyze the error message
It says no matching request was found for '/api/items'.
Step 2: Understand expectOne behavior
expectOne throws if no request matches the URL, meaning no request was made.
Final Answer:
The tested service did not make any HTTP request to '/api/items' -> Option B
Quick Check:
No matching request means no HTTP call made = C [OK]
Hint: No matching request means tested code didn't call that URL [OK]
Common Mistakes:
Assuming verify() missing causes this error
Thinking injection failure causes this error
Ignoring URL typos in expectOne
5. In a test, you want to verify that exactly one GET request to '/api/users' was made and respond with mock data. Which code snippet correctly does this using HttpTestingController?
hard
A. const req = httpMock.expectOne('/api/users');
req.error(new ErrorEvent('Network error'));
httpMock.verify();
B. httpMock.expectNone('/api/users');
httpMock.verify();