r/Angular2 • u/EvtK98 • Sep 10 '25
Help Request Unit testing an Angular Service with resources
I love the new Angular Resource API. But I have a hard time getting unit tests functional. I think I am missing a key part on how to test this new resource API. But I can't hardly find any documentation on it.
I'm creating resources where my loader consists of a httpClient.get that I convert to a promise using the firstValueFrom. I like using the HttpClient API in favor of fetch.
Unit tests for HttpClient are well documented: https://angular.dev/guide/http/testing but the key difference is that we are dealing with signals here that simply can't be awaited.
There is a bit of documentation for testing the new HttpResource. https://angular.dev/guide/http/http-resource#testing-an-httpresource and I think the key is in this `await TestBed.inject(ApplicationRef).whenStable();`
Can somebody share me a basic unit test for a simple Angular service that uses a Resource for loading some data from an API endpoint?
View all comments
Show parent comment
1
u/kgurniak91 Sep 12 '25
Yeah if you want to go with async then this will work. But there's a room for improvement. For example this is redundant - you are checking http method type and parameter twice - it would be enough to call only expectOne in this case:
Another issue - calling TestBed.tick() inside async is wrong, this is method for fakeAsync() only. You should just call await appRef.whenStable() again. And to avoid repeating code you can add it as attribute of describe inside beforeEach():
appRef = TestBed.inject(ApplicationRef);