2017-11-25 290 views
1

我試圖單元測試一個服務,我有可以響應http調用。我可以測試成功請求,但是當響應的狀態代碼與200不同時,我無法進行測試。角單元測試失敗HttpClient請求

例如,讓我們假設請求返回404狀態,然後我無法正確測試。

這是我的服務:

@Injectable() 
export class ApiService { 
    constructor(
     private _http: HttpClient, 
     private _router: Router, 
     private _toast: ToastsManager, 
     private _auth: AuthService, 
    ) { } 

    public apiGet(url: string) { 
     return this._http 
      .get(url) 
      .catch(this.handleError.bind(this)); 
    } 

    private handleError(error) { 
     if (error.status === 401) { 
      this._auth.logout(); 
      return Observable.throw(error); 
     } 

     if (error.status === 404) { 
      this._router.navigateByUrl('not-found'); 
      return Observable.throw(error); 
     } 

     if (error.error && error.error.message) { 
      this._toast.error(error.error.message); 
     } else { 
      this._toast.error('Something went wrong'); 
     } 

     return Observable.throw(error); 
    } 
} 

這就是我是如何測試:

describe('ApiService',() => { 
    let service: ApiService; 
    let backend: HttpTestingController; 

    const mockSuccessResponse = { value: '123', name: 'John' }; 
    const mockSuccessStatus = { status: 200, statusText: 'Ok' }; 

    beforeEach(() => { 
     TestBed.configureTestingModule({ 
      imports: [ 
       MockModule, 
       HttpClientTestingModule, 
      ], 
      providers: [ 
       ApiService, 
      ] 
     }); 

     service = TestBed.get(ApiService); 
     backend = TestBed.get(HttpTestingController); 
    }); 

    it('should call the apiGet() function with success',() => { 
     service.apiGet('mock/get/url').subscribe(response => { 
      expect(response).toEqual(mockSuccessResponse); 
     }); 

     const req = backend.expectOne('mock/get/url'); 

     expect(req.request.url).toBe('mock/get/url'); 
     expect(req.request.method).toBe('GET'); 

     req.flush(mockSuccessResponse, mockSuccessStatus); 
    }); 

    it('should execute handleError function on status different of 200',() => { 
     service.apiGet('mock/error/url').subscribe(response => { }, error => { 
      // Handle the error cases here (?) 
     }); 

     const req = backend.expectOne('mock/error/url'); 

     req.flush(null, { status: 404, statusText: 'Not Found' }); 
    }); 

    afterEach(() => { 
     backend.verify(); 
    }); 
}); 

我不知道如何從這裏繼續。如果我嘗試執行類似expect(service.handleError()).toHaveBeenCalled();的操作,我會收到類似handleError is a private method的錯誤。

我還需要測試authService上的函數logout()是否會被調用,或者如果路徑更改爲not-found 404錯誤。

我應該怎麼做才能測試這些情況,其中響應的狀態不同於200?

回答

0

我解決這個問題的方法是測試handleError函數是否被調用,然後創建另一組測試來單獨測試handleError函數。

it('should execute handleError function on status different of 200',() => { 
    // "as any" to avoid the "private method" type errors 
    spyOn(service as any, 'handleError'); 

    service.apiGet('mock/get/url').subscribe(() => { }, error => { 
     // This is where I just check if the function was called 
     // It also needs to be service['handleError'] to avoid type errors on private method 
     expect(service['handleError']).toHaveBeenCalled(); 
    }); 

    const req = backend.expectOne('mock/get/url'); 

    req.flush(null, { status: 400, statusText: 'Error' }); 
}); 

然後,我創建另一組測試,我手動設置我試圖測試,404例如錯誤代碼:

it('should navigate to route "not-found" on status 404',() => { 
    spyOn(router, 'navigateByUrl'); 

    const mockError = { 
     status: 404, 
     error: { 
      message: 'Page not found', 
     }, 
    }; 

    service['handleError'](mockError); 

    // Here we adapt to test whatever condition it is 
    // Mine is a simple redirect to an error page 
    expect(router.navigateByUrl).toHaveBeenCalledWith('not-found'); 
});