Angular Jasmine - Test case gets failed in http HE

2019-08-19 03:55发布

I'm trying to write a test case for the HTTP HEAD Request, it works fine without flush method. If I triggered the flush method the chrome browser gets disconnect. I don't know what needs to pass as a param to the flush method.

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

    });
}

Test Case Spec file:

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 response = service.processData(id);

        const req = httpMock.expectOne({ url: filePath, method: 'HEAD'});
        httpMock.verify();

        expect(response).toBeUndefined();

        req.flush({}, { headers: { 'Content-Type': 'application/json' } });

        // req.flush(null);
    })
}

Chrome Browser's Screenshot

enter image description here

If I commented the flush method, subscribe won't execute. So, kindly assist me how to handle flush method in HEAD request.

0条回答
登录 后发表回答