TypeError:无法读取 angular 中 jasmine 测试用例中未定义的属性(读取“toString”)

TypeError: Cannot read properties of undefined (reading 'toString') in jasmine test case in angular

提问人:New tech User 提问时间:11/16/2023 最后编辑:New tech User 更新时间:11/16/2023 访问量:39

问:

'我是 angular Jasmine 测试用例的新手,我的文件中有 ngOninit component.ts 当我在里面运行component.ngOnInit()时,应该创建函数。它给出错误 TypeError:无法读取 undefined 的属性(读取“to String”)

在此处输入图像描述

在此处输入图像描述

添加了代码图像。任何人都请帮忙

export class TestPageComponent implements OnInit {

  subjectArray: letterPageType[] = [];

  dependents: DependentVO[];
  associate: ProfileVO;

  constructor(
    public profileService: ProfileService,
    public dependentService: DependentService,
    public loginService: LoginService,
    public benefitsUtil: BenefitsUtil,
    public dialog: MatDialog,
    public utilMethods: viewMemosUtil,
    public applicationErrorHandler: ApplicationErrorHandler,
    public gLetterService: GLetterService,
    public logger: NGXLogger,
    public getAssociatesService: GetAssociatesService
  ) { }

  ngOnInit(): void {
    this.intialize();
  }

  intialize() {
    this.dependents = this.dependentService.dependents;
    this.getAssociatesService.currentAssociate.subscribe(currentAssociate => {
      this.associate = currentAssociate;
    })
    let personObject: letterPageType = new letterPageType();
    personObject.name = this.benefitsUtil.getFullName(this.associate.firstName, this.associate.middleInitial,this.associate.lastName);
    personObject.id = this.associate.bid.toString();
    personObject.checked = false;
    this.subjectArray.push(personObject);
    for(const person of this.dependents) {
      personObject = new letterPageType();
      personObject.name = this.benefitsUtil.getFullName(person.firstName, person.middleInitial, person.lastName);
      personObject.id = person.personId;
      personObject.checked = false;
      this.subjectArray.push(personObject);
    }
    personObject = new letterPageType();
    personObject.name = "Others"
    personObject.id = "0";
    personObject.checked = false;
    this.subjectArray.push(personObject);
    this.recipientsNumber = this.subjectArray.length;
  }
}



    describe('TestPageComponent', () => {

let component: LettersPageComponent;
let fixture: ComponentFixture\<LettersPageComponent\>;
let dialog:MatDialog;
let dialogRef:jasmine.SpyObj\<MatDialogRef\<LettersPageComponent\>\>;
let dependentService:DependentService
let getAssociatesService:GetAssociatesService
let benefits:BenefitsUtil;
let profileService:ProfileService;
let  gLetterService: GLetterService;
let utilService:viewMemosUtil;

beforeEach(async () =\> {
dialogRef=jasmine.createSpyObj('MatDialogRef',\['close'\]);
let getAssociatesServicestub = () =\> ({
currentAssociate: () =\> ({ subscribe: () =\> ({}) })
});

    await TestBed.configureTestingModule({
      imports:[TranslateModule.forRoot(),MatDialogModule,BrowserAnimationsModule],
      declarations: [ LettersPageComponent,PaginatePipe ],
      providers:[ProfileService,GLetterService, HttpClient,HttpHandler,DependentService,LoginService,HttpBackend,LoggerConfig,
        DatePipe,ApplicationErrorHandler,MatDialog,Overlay,BenefitsUtil,OptionRateService,HistoryCoveragesService,
        CoveragesService, PlanEligibleRulesService,MatSnackBar,DecimalPipe,viewMemosUtil,GetAssociatesService,PaginatePipe,
        PaginationService, { provide: getAssociatesService, useFactory: getAssociatesServicestub }
      ],
      schemas:[CUSTOM_ELEMENTS_SCHEMA]
    })
    .compileComponents();

});

beforeEach(() =\> {
fixture = TestBed.createComponent(LettersPageComponent);
component = fixture.componentInstance;
dependentService=TestBed.inject(DependentService);
profileService=TestBed.inject(ProfileService);
dialog=TestBed.inject(MatDialog)

    benefits=TestBed.inject(BenefitsUtil)

component.associate = new ProfileVO();
component.associate.bid=123;
fixture.detectChanges();
});

it('should create', () =\> {
let currentAssociate={bid:23,firstName:''};
let service = TestBed.get(getAssociatesService);
spyOn(service, 'currentAssociate').and.returnValue(of(currentAssociate))

    expect(component.subjectArray).toEqual([]);
    component.fetchLetters();
    fixture.whenStable().then(() => {
      expect(component).toBeTruthy();

});

});
}`
业力-茉莉花

评论

0赞 New tech User 11/16/2023
谁能帮帮我

答: 暂无答案