Unit testing 组件中http get方法的单元测试

Unit testing 组件中http get方法的单元测试,unit-testing,angular5,Unit Testing,Angular5,我对测试和角度5是新手。 我有一个组件函数,它依次调用http get方法和retuns json数据。 我想测试一下。 这是我的组件函数片段 this.enablebutton=false; this.users=[]; getUsers(){ this.http.get(url).subscribe(result=>{this.users=result if(this.users.length>0){ this.enablebutton=true;

我对测试和角度5是新手。 我有一个组件函数,它依次调用http get方法和retuns json数据。 我想测试一下。 这是我的组件函数片段

this.enablebutton=false;
this.users=[];
getUsers(){
   this.http.get(url).subscribe(result=>{this.users=result

     if(this.users.length>0){
      this.enablebutton=true;
    }
   })
}
在教程中,我发现它们有一个单独的服务层,用于调用web服务来获取这样的数据,我们有测试用例,但我目前没有使用任何服务

 this.userService.getUsers().subscribe(result=> this.users= result);
我把我的测试用例写为

beforeEach(() => {

    fixture = TestBed.createComponent(AppComponent);

    component = fixture.componentInstance;
  //  spyOn(component, "getUsers").and.callThrough();
      component.getUsers();
    fixture.detectChanges();
  });
我不知道如何手动设置http.get调用的结果值,并测试这个依赖于http调用结果的.enablebutton的条件。
谢谢。

第一步:正确缩进代码,使其可读。第二步:不要窥探你想要测试的方法:这没有意义,因为它用一个不做任何事情的方法替换了你想要测试的方法。第三步:阅读以了解如何模拟Http。如何在Http.get方法上直接使用模拟Http。您也可以通过监视Http.get来实现这一点。阅读文档,然后尝试一下。如果您被卡住了,请准确地告诉我您尝试了什么以及您遇到了什么问题。谢谢@JBNizet您的评论非常有用,我的阻止程序已被删除,我可以开始测试:)