Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular8 Unitest error: 'Automatic conversion to Blob is not supported for response type.'

I am writing an httpGet call to get images as blob here is the call:

      public getImage(link: string): Observable<any> {
    
        return this.http.get<any>(this.createUrl('storage/' + link ), { headers: this.createHeaders(), 
        responseType: 'blob' as 'json' });
      }

This is my unittest:

  it('should get the images', () => {
    let response = {};
    service.getImage('testImage').subscribe(
      data => {
        expect(data).toEqual(response, 'should return expected data');

        const req = httpMock.expectOne(`https://api/storage/testImage`);
        expect(req.request.method).toBe('GET');
        req.flush(response);
     
      });     
  });

When I write the test like above I get the error: "Expected no open requests, found 1:"

If I put this code:

const req = httpMock.expectOne(https://api/storage/testImage); expect(req.request.method).toBe('GET'); req.flush(response);

outside of the subscription I get the error : "Automatic conversion to Blob is not supported for response type."

Does someone knows how to fix this?

like image 956
marko07 Avatar asked Aug 27 '20 11:08

marko07


1 Answers

I changed the response to blob type:

 let response = new Blob();

and it works like a charm 😋

like image 58
Saad Avatar answered Sep 28 '22 07:09

Saad