【问题标题】:TypeError: cannot read properties of undefined (reading 'split')TypeError:无法读取未定义的属性(读取“拆分”)
【发布时间】:2022-01-18 16:06:46
【问题描述】:

我是 Angular 单元测试(Jasmine karma)的新手。我收到 TypeError: cannot read properties of undefined (reading 'split') 。如何解决?提前致谢。

ts 文件:

export class SelectedComponent extends OnInit {
paidReference: string; 
timeReference: string;
constructor(public activatedRoute: ActivatedRoute) {} 
ngOnInit() { let tempData=
this.activatedRoute.snapshot.queryParams.Ref.split('-');
this.paidReference = tempData[0]; }
} 

spec file: 

describe('SelectedComponent', () => {
let component:SelectedComponent;
let fixture:ComponentFixture<SelectedComponent
    >; beforeEach(async(() => { TestBed.ConfigureTestingModule({
    declarations: [SelectedComponent],
    schemas:[CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
    imports:[modules],
    providers:[{provide: ActivatedRoute, UseValue: {**snapshot: {queryParams:
    {Ref: '123456-15/12/2021'**}}} }]
    }).compileComponents();
    }))

    beforeEach(() => { fixture = TestBed.createComponent(SelectedComponent);
    component = fixture.componentInstance;
    fixture.detectChanges(); }) 

    it('should create', () => {
    expect(component).toBeTruthy();
    })
    })

【问题讨论】:

    标签: angular karma-jasmine


    【解决方案1】:

    试试

    let tempData= this.activatedRoute.snapshot.queryParams.params.Ref.split('-');
    

    【讨论】:

    • 它不起作用。实际上我的错误是在单元测试期间 - ng 测试。
    【解决方案2】:

    尝试将.Ref 更改为['Ref']

    this.activatedRoute.snapshot.queryParams['Ref'].split('-')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2023-03-24
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      相关资源
      最近更新 更多