订阅无法在茉莉花业力测试中工作(未定义)

时间:2019-12-12 14:10:52

标签: jasmine karma-jasmine

我不确定为什么下面的测试中对服务调用的订阅显示未定义错误

错误:TypeError:无法读取未定义的属性“ subscribe”

规格:

it('should get lead states and counties', inject(
    [AgentLeadsService, HttpTestingController], (service: AgentLeadsService, controller: HttpTestingController) => {
      // arrange
      let states: Array<string> = [];
      let counties: Array<string> = [];

      // act
      let spyC = spyOn(component, 'getLeadStatesAndCounties');
      let spyS = spyOn(service, 'getLeadStatesAndCounties');
      component.getLeadStatesAndCounties();
      service.getLeadStatesAndCounties();
      // TODO: return mock obj when sub is called
      service.getLeadStatesAndCounties().subscribe();
      const req = controller.expectOne('/leads-states-counties');
      req.flush({states: component.states, counties: component.counties});
      controller.verify();
      fixture.detectChanges();

      // assert
      expect(spyC).toHaveBeenCalledTimes(1);
      expect(spyS).toHaveBeenCalledTimes(1);
      expect(component.states).not.toBeNull();
      expect(component.counties).not.toBeNull();
      expect(states).not.toBeNull();
      expect(counties).not.toBeNull();
    }));

服务:

 getLeadStatesAndCounties() {
    return this.http.get<LeadStatesAndCounties>(environment.apiURL + '/leads-states-counties');
  }

0 个答案:

没有答案