Angular Jasmine - Test case gets failed in http HEAD request while on Flush

落花浮王杯 提交于 2019-12-11 19:45:28

问题


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

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

来源:https://stackoverflow.com/questions/57001238/angular-jasmine-test-case-gets-failed-in-http-head-request-while-on-flush

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!