I know that I should test a REST server from Angular with a mock, and there are dozens of examples on the web how to do this. But assume I want to test a service on the real thing which I have full control over.
Here is the working mocked version:
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
})
});
});
How do I redirect this to use the REST server instead of the mock? The code below does not work because I don't know how to create HttpClient for 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()
});
});
});
Aucun commentaire:
Enregistrer un commentaire