首页 > 解决方案 > ctor 不是构造函数。Angular7 单元测试

问题描述

我正在为使用服务的组件编写单元测试用例。在测试时我收到以下错误:

ctor is not a constructor

如何解决此错误?

ctor 不是构造函数 - 错误截图

标签: angularjasmineangular7

解决方案


在 beforeEach 函数中替换useClassuseValue, 内部提供程序。

  beforeEach(async(() => {
    service = jasmine.createSpyObj('ContactService', ['getContacts']);
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule],
      declarations: [CityComponent],
      providers: [{provide: ContactService, useValue: service}]
    })
    .compileComponents();
  }));

推荐阅读