1

I have the following method


    getWsById(idWs: number, isCache: boolean = true): Observable<WSModel> {
        try {
            if (isCache) {
                const answer: WSModel = this.fullWSModelCache$
                    .getValue()
                    .filter((fullWS) => fullWS.workspace.workspaceId === idWs)
                    .map((fullWS) => fullWS.workspace)[0];
                return of(answer);
            }

            const params = new HttpParams().set('idWs', idWs.toString());
            const wsResponse = this.http.get<WSModel>('rest/ws/ws-by-id', {
                params
            });



            return wsResponse;
        } catch (error) {
            this.updateHasError(true);
            console.error(`Error occurred: ${error}`);
        }
    }

I want to write a unit test that check the cache mode and verify that there was no Rest call. How to achieve this ? I dont have the slightest idea!!!

My current test is like below

    it("should test getWsById cache mode", () => {

        wsService.updateFullWsModelCache(currentFullWSModelCache);
        wsService.getWsById(testingWsModel.workspaceId).subscribe( res => expect(res).toEqual(testingWsModel));


        
        const req = httpTestingController.expectOne({
            method: 'GET'
        });

        //expect(req.countOfCalls).toEqual(0); how to achieve this


    });

1 Answers1

0

I will do some refactoring of my service methode where I extract the cache utility in a function/method and extract the Rest call utility in another function/method

Then will check if the method/function have been called

Thanks to this answer I got the IDEA