Я знаю, что я должен протестировать REST-сервер из Angular с помощью макета, и в Интернете есть десятки примеров, как это сделать. Но предположим, что я хочу протестировать сервис на реальной вещи, над которой у меня есть полный контроль.
Вот рабочая макетная версия:
describe('SgformsService', () => {
let sgformsService: SgformsService
let httpMock: HttpTestingController
beforeEach(() => {
TestBed.configureTestingModule({
providers: [SgformsService],
imports: [HttpClientTestingModule]
})
httpMock = TestBed.get(HttpTestingController);
sgformsService = TestBed.get(SgformsService);
});
it('should return a single item when mocked', () => {
sgformsService.getSingle().subscribe(
(sg: SgFormsBase) => {
expect(sg.id).toEqual(34)
expect(sg.pat_no).toEqual(1704)
})
const req = httpMock.expectOne(
'http://localhost:8000/getsingle/Endoskopie/1631/2019-03-19/arzt/0', 'call to api');
expect(req.request.method).toBe('GET');
req.flush({
id: 34,
pat_no: 1704
})
});
});
Как мне перенаправить это, чтобы использовать REST-сервер вместо макета? Код ниже не работает, потому что я не знаю, как создать HttpClient для SgformsService
.
describe('SgformsService without Mocking', () => {
let sgformsService: SgformsService
//let httpClient: HttpClient
beforeEach(function() {
// httpClient = new HttpClient()
// sgformsService = new SgformsService(httpClient);
});
// https://angular.io/guide/testing#service-tests
it('should return a single item from server',
(done: DoneFn) => {
sgformsService.getSingle().subscribe(
(sg: SgFormsBase) => {
expect(sg.id).toEqual(34)
expect(sg.pat_no).toEqual(1704)
done()
});
});
});