簡體   English   中英

jasmine 測試服務 angular - 找不到 object 來監視 getBaseUrl()

[英]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'找不到getBaseUrl() ,非常感謝任何幫助。 這是stackblitz的鏈接

您忘記添加:

apiEndpoints = TestBed.get(ApiEndpointsConfig);
 

到 test.service.spec.ts 中的 beforeEach

這將解決您的問題。 但是你會得到另一個與測試本身相關的錯誤;)如果你不能處理另一個錯誤,給我打電話

這是因為服務中使用的 url 與 expectOne 中使用的 url 不同。 要消除此錯誤,您可以執行以下操作。 將 api-endpoints.config 中的 url 更改為。

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

然后將 expectOne 中的 url 更改為如下:-

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

只有這樣你才會有匹配的 url,expectOne 才會成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM