I would like to only test if a request is being sent to the correct API endpoint. For example: I have a service that sends a request to http://localhost:port/sth/sth2
and I want to check only if there was http://localhost:port
. I'm using HttpTestingController
and the expectOne
function, is it possible to do?
Is this the sort of thing you were looking to do?
Setup
let httpClient: HttpClient;
let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule ]
});
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
});
Test
it('can test HttpClient.get', () => {
const testData: Data = {name: 'Test Data'};
httpClient.get<Data>('http://localhost:8085/test/suite')
.subscribe(data =>
expect(data).toEqual(testData)
);
const request = httpTestingController.expectOne(
(req: HttpRequest<any>) => req.url.includes('http://localhost:8085'));
request.flush(testData);
httpTestingController.verify();
});
It's the example from Angular's documentation, except you pass a function to expectOne
, that takes a req and returns a boolean that checks if the url of the request contains the right endpoint.
Of course, you'll swap out the HttpClient with your service.