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?