I'm new to angular unit testing. What I want to do is getting actual results from my API. I checked this documentation but as I understand, I should create mock responses. Here is my code,
myService.ts:
...
public GetAll = (apiname: string): Observable<Object> => {
return this._http.get("foo/" + apiname,{headers:this.options}); //this.options = newHttpHeaders({'ContentType':'application/json','Accept':'application/json'});
}
...
myService.spec.ts:
...
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientModule,
HttpClientTestingModule
],
providers: [
MyService
]
});
});
afterEach(inject([HttpTestingController], (backend: HttpTestingController) => {
backend.verify();
}));
it('GetAll() should work', () => {
const testData = {id: "123"};
service.GetAll("items").subscribe(data => {
//console.log(JSON.stringify(data));
});
const req = httpTestingController.expectOne('foo/items');
expect(req.request.method).toEqual('GET');
req.flush(testData);
httpTestingController.verify();
});
When I write the result in console, it writes 'testData' instead of 'data'. As I said before, I want to get actual result from my API, not mock result. Sorry if it is a silly question but how can I do that ? Thanks for advice.