I'm having a service method and it has a service call (HTTP call) and it subscribes immediately and based on the response code it executes the rest of the action command.
Example: Service Method
processData(id): void {
const url = `http://localhost:5000/data/${id}`;
this.http.head(url).subscribe(() => {
console.log('Success');
// Rest of the code - TODO
}, (error) => {
console.log('Failed');
// Rest of the code - TODO
});
}
I tried the following sample (Test case)
fdescribe('ReportedFileService', () => {
let service: DataService;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports:[HttpClientModule, HttpClientTestingModule],
providers:[DataService]
});
service = TestBed.get(DataService);
httpMock = TestBed.get(HttpTestingController);
});
afterEach(() => {
httpMock.verify();
});
fit('should be valid', () => {
const id = 1;
const filePath = `http://localhost:5000/data/${id}`;
const req = httpMock.expectOne(filePath);
expect(req.request.method).toEqual('Head');
const response = service.processData(id);
})
}
kindly assist me how to handle this situation.