试图订阅测试文件中的http调用,我需要在测试中声明可观察者

问题描述 投票:-1回答:2

我需要在测试中进行实际的Http调用,并且需要验证我的可观察性。

当我调试代码时,在serviceice中调用了Http调用,但是在我的测试中失败了,并且它说http是未定义的,但是在调试时,我能够在控制台中看到http观察。 s

          //service.ts file 
         //imports modles goes here 



  export class DataService {
      private api = this.configService.config.apiUrl;

  constructor(private http: HttpClient,
        private configService: AppConfigService,
        private errorService: ErrorService) 
          { 
          }
          public getCustomerList(): Observable<IUserResponse> {
                     return this.http.get<IUserResponse>`${this.api}/v1/getusers/users`);
      }

    }


 my test file serviec.specs.ts

  describe('OnboardingService', () => {
      beforeEach(() => {

     const httpClient = new HttpClient(new HttpXhrBackend({ build: () => new XMLHttpRequest() }));

    appConfigService= new AppConfigService(httpClient);

    appConfigService.config = { apiUrl:"https://test-test-getuser.json"}

    erroService = new ErrorService();

   service = new Dataservice(httpClient, appConfigService,erroService);
   it('should return an Observable<Iuser[]>', done => {

               service.getCustomerList().subscribe(response => {
          expect(response.users).toBeGreaterThan(0);
          done();
        });


    });
})
javascript node.js angular angular6
2个回答
0
投票

尝试一下

this.http.get(`${this.apiurl}/v1/getuser`).subscribe((response)=>{console.log(response.user)})

0
投票

enter image description here

灰色右箭头显示要执行的代码。下面的左灰色箭头显示了执行结果。控制台日志在开发工具中没有箭头。

enter image description here

执行结果.subscribe()是一个订阅服务器,您可以在屏幕快照中注意到。您的代码只是不执行传递给.subscribe()的功能。

所以您的问题可能出在端点上。检查开发工具中的“网络”标签。

© www.soinside.com 2019 - 2024. All rights reserved.