Reputation: 477
I have this unit test for this observable:
fdescribe('goToPurchases', () => {
it('should have working NavApiService',() => {
apiService.goToPurchases(null).subscribe((res: PurchaseResponse) => {
expect(res.shimentId).toBe('not supposed to be valid!')
expect(res.url).not.toBeDefined();
});
});
});
this is real method in apiService.ts:
public goToPurchases(shipmentId: string): Observable<Object> {
return this.httpClient.post(`${environmentVariables.loadInsurance.apiUrl}/v1/purchases`, {shipmentId});
}
The real response should be something like this:
{
"id": "50d4ad69-0337-4e49-8f00-5c9a152cfad0",
"shipmentId": null,
"userId": "41ac2d3d-b2ed-4c73-9c56-25d17a9caa7e",
"url": "https://test.portal.loadsure.net/insureLoad/v2?id=TFMtREFULzIwMjEwODMxL3l6ZDVlYlJRejdrQkdKcUwxbk5l"
}
When I run the test it passes successfully? What am I doing wrong?
Thanks.
Upvotes: 0
Views: 655
Reputation: 4228
By having the expectation into the subscribe callback, you are correctly able to evaluate it once a value is retrieved. The problem is the test completes before waiting for the response. By completing this way and as it doesn't evaluate anything, the test is a success.
You need to change the way the test completes.
You can use fakeasync
so your test will only complete once all async tasks are done:
import { fakeAsync } from '@angular/core/testing';
fdescribe('goToPurchases', fakeAsync(() => {
it('should have working NavApiService',() => {
apiService.goToPurchases(null).subscribe((res: PurchaseResponse) => {
expect(res.shimentId).toBe('not supposed to be valid!')
expect(res.url).not.toBeDefined();
});
});
}));
Upvotes: 2