简体   繁体   中英

jasmine testing service angular - could not find an object to spy upon for getBaseUrl()

I am new to jasmine unit test service in angular, trying to achieve unit test service which has simple get method with Observable as a return, for which I am getting the URL from another file, in this ApiEndpointsConfig but for some reason, I couldn't find the getBaseUrl() , any help is much appreciated. here is the link for stackblitz

You forget to add:

apiEndpoints = TestBed.get(ApiEndpointsConfig);
 

to your beforeEach in test.service.spec.ts

This will solve your problem. But you'll get another error linked to the test itself;) If you can't handle another one, give me a call

This is because the url used in the service is different from the url that is used in the expectOne. To counteract this error, you can do the following. Change the url in api-endpoints.config to be.

 public get getBaseUrl(){
    return 'http://example.com/app/data';
  }

And then change the url in the expectOne to be as the following:-

const req = httpMock.expectOne(spy() + '/app/data');

Only then you will have matching urls and the expectOne would succeed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM