首页 > 解决方案 > TypeError:无法读取未定义的属性“get”(Angular getter)

问题描述

运行测试规范时在 ts 中出现错误。角度获取函数()。我曾尝试在该物业上创建一个间谍,但没有成功..

规格:

const spyC = spyOnProperty(component, 'contractId').and.returnValue(new 
    FormGroup({}));

或者

const spyC = spyOnProperty(component, 'contractId').and.callThrough();
    expect(spyC).toHaveBeenCalled();

TS:

get contractId() {
        return this.formService.queryParamSnapshot.get('contractId');
      }

在此处输入图像描述

标签: angularjasminekarma-jasmine

解决方案


似乎必须与提供者打交道:

{provide: AuthService, useClass: MockAuthService}

const fakeActivatedRoute = {
    snapshot: {data: {}}
  } as ActivatedRoute;

推荐阅读