[英]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. 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'找不到getBaseUrl()
,非常感谢任何帮助。 here is the link for stackblitz这是stackblitz的链接
You forget to add:您忘记添加:
apiEndpoints = TestBed.get(ApiEndpointsConfig);
to your beforeEach in test.service.spec.ts到 test.service.spec.ts 中的 beforeEach
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.这是因为服务中使用的 url 与 expectOne 中使用的 url 不同。 To counteract this error, you can do the following.要消除此错误,您可以执行以下操作。 Change the url in api-endpoints.config to be.将 api-endpoints.config 中的 url 更改为。
public get getBaseUrl(){
return 'http://example.com/app/data';
}
And then change the url in the expectOne to be as the following:-然后将 expectOne 中的 url 更改为如下:-
const req = httpMock.expectOne(spy() + '/app/data');
Only then you will have matching urls and the expectOne would succeed.只有这样你才会有匹配的 url,expectOne 才会成功。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.