Angular HttpTestingController Unable to match url with params

202 views Asked by At

I am new to HttpTesting controller. my expectations not working as the url contains HttpParams. here is my service, and spec.ts

hasAccessControlException() {
        const apiPath = `https://api.example.com/v1/session/permissions/exceptions/hasAccessControlException/`;

        let params = new HttpParams();
        params = params.append('auditInstanceId', 123);
        
        return this.http
            .get<boolean>(apiPath, {
                params: params,
                observe: 'response'
            })
            .pipe(
                map((response) => {
                    return response.body;
                })
            );      

}

describe('PermissionExceptionService', () => {
    let httpTestingController: HttpTestingController;
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [
                PermissionExceptionService,
            ]            
        });
        service = TestBed.inject(PermissionExceptionService);
        httpTestingController = TestBed.inject(HttpTestingController);  
    })

    fit('should access control', () => {
        service.hasAccessControlException();

        const url = `https://api.example.com/v1/session/permissions/exceptions/hasAccessControlException/`;
        
         const request = httpTestingController.expectOne((req) => {
             expect(req.url).toBe(url);
             return true;
         });
        req.flush(null);

        httpTestingController.verify();     
    })

});

I am getting the following error:

Error: Expected one matching request for criteria "Match by function: ", found none
1

There are 1 answers

0
Mukil Deepthi On

The above code worked, when the service call is subscribed. i.e

service.hasAccessControlException().subscribe();