Complete the code to import the HttpTestingController for testing HTTP calls.
import { TestBed } from '@angular/core/testing'; import { HttpClientTestingModule, [1] } from '@angular/common/http/testing';
The HttpTestingController is imported from @angular/common/http/testing to mock HTTP requests in tests.
Complete the code to inject HttpTestingController in the test setup.
let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
});
httpTestingController = TestBed.[1](HttpTestingController);
});Use TestBed.get() (or TestBed.inject() in newer Angular versions) to retrieve the HttpTestingController instance.
Fix the error in the test by completing the code to expect one HTTP GET request.
const req = httpTestingController.[1]('api/data');
The expectOne method expects exactly one HTTP request matching the URL.
Fill both blanks to flush a mock response and verify no outstanding requests.
req.[1]({ data: 'test' }); httpTestingController.[2]();
Use flush to send a mock response and verify to ensure no pending requests remain.
Fill all three blanks to test an HTTP POST request and respond with mock data.
service.postData({name: 'test'}).subscribe(response => {
expect(response).toEqual([1]);
});
const req = httpTestingController.[2]('api/post');
req.[3]({id: 1, name: 'test'});The test expects the mock response object, uses expectOne to find the POST request, and flush to send the mock response.