繁体   English   中英

TypeError: undefined is not a constructor (评估 'this.service.getDat().subscribe')

[英]TypeError: undefined is not a constructor (evaluating 'this.service.getDat().subscribe')

我正在开发一个 Angular 4 应用程序,编写单元案例,我面临以下问题。

组件.ts

 ngOnInit() {
    this.service.getData().subscribe(res => {
      console.log(res);
      this.data= JSON.parse(res._body);
      this.addFormControls();
    },
      error => {
        console.log(error);
      });
  }

组件.spec.ts

 describe('component', () => {

      let userRolesService: LayoutService;
      const modulesData = {
        'moduleName':'Info'
      }
      class MockUserRolesService {
        public getModulesData() {
            return modulesData;
        }
      }
      beforeEach(async(() => {
        TestBed.configureTestingModule({
          providers:[ {provide: LayoutService, useClass: MockUserRolesService } ]
        })
        .compileComponents();
      }));

      beforeEach(() => {
        userRolesService = TestBed.get(LayoutService);

      });
      afterEach(() => {
        userRolesService = null;
       component = null;
     });


        it('should fetch module data', () => {

        userRolesService.getData().subscribe(
          result => {
          expect(result).toBeTruthy();
          }
      );
      });
    });

更新了模拟服务文件的代码。你能帮我一个人让它工作吗? 使用模拟服务和测试床框架。 你能请人帮帮我吗…………………………………………………………………………………… ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... ...................................................

我不知道你试图模拟什么,但这不是你模拟服务的方式。

我将使用useValue而不是useClass ,但您可以根据需要进行调整。

您的测试正在测试对 observable 的订阅,然后您返回……什么都没有。

class MockUserRolesService {
  public getModulesData() {
    return modulesData;
  }
}

您正在测试getData ,但我在您的模拟中看不到它。

以下是模拟服务的方式。

const serviceMock = {
  provide: LayoutService,
  useValue: {
    getData: Observable.of(/* any value you want */)
  }
};

TestBed.configureTestingModule({
  providers:[ {provide: LayoutService, useValue: serviceMock } ]
})

编辑在单元测试中,您测试您的功能(组件、服务)是否按预期工作。 让我们以您的ngOnInit函数为例:

this.service.getData().subscribe(res => {
  console.log(res);
  this.data= JSON.parse(res._body);
  this.addFormControls();
}

鉴于您编写它的方式,您的测试应涵盖以下情况:

  • 我的组件是否在服务上调用getData
  • 成功后,我的组件是否存储服务返回的数据?
  • 控制台是否显示数据?
  • 是否调用了addFormControls方法?

如果你想测试它,你应该创建这个模拟:

useValue: {
  getData: Observable.of({
    '_body': 'my data'
  })
}

现在,您必须进行测试。 由于您有一个异步调用(使用 Observable),那么您应该订阅该调用,然后调用。 这是它是如何完成的。

it('should call the getData function of the service and store the data', () => {
  // Spy on your service to see if it has been called, and let him process
  spyOn(component['service'], 'getData').and.callThrough();
  // Array notation avoid scope issues (if your variable is protected or private)
  component['service'].getData().subscribe(response => {
    expect(component['service'].getData).toHaveBeenCalled();
    // Since you store the data in the data variable, check if it's equal to your mock value
    expect(component.data).toEqual('my data');
  });
});

如果要测试所有情况:

it('should call the getData function of the service and store the data', () => {
  spyOn(component['service'], 'getData').and.callThrough();
  spyOn(component, 'addFormControls');
  spyOn(console, 'log');
  component['service'].getData().subscribe(response => {
    expect(component['service'].getData).toHaveBeenCalled();
    expect(component.data).toEqual('my data');
    expect(console.log).toHaveBeenCalledWith(response);
    expect(component.addFormControls).toHaveBeenCalled();
  });
});

我遇到了类似的问题,但在地图内

undefined 不是评估 arr.uniorgs 的构造函数

所以这:

this.arr.map(aux => aux.uniorgs)

我改为:

this.arr.map(aux => aux['uniorgs'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM