获得错误无法在角度单元测试中设置未定义的属性

发布于 2025-01-31 18:22:31 字数 1034 浏览 5 评论 0原文

当我编写单元测试案例以进行下拉列表时,我会遇到错误 typeError:无法设置未定义的属性(设置“转码”) .spec.ts

it("should update the action selecting a value from category drop down", () => {
    component.actionCodesLookupData.ReferralCodes =[
      {
      Name: "some data",
      Id: 3,
      ActionStatus: 126
    }
    ]
    component.setActionCodesList("Referral");
    expect(component.actionCodesList).toBe(component.actionCodesLookupData.ReferralCodes);
       });

.component.ts

 public setActionCodesList(actionType: any):void{
   this.actionCodesList = [];
 
    switch(actionType){
      case ActionRecordConstant.memberContact:
      this.actionCodesList = this.actionCodesLookupData.MemberContactCodes;
     break;
      case ActionRecordConstant.referral:
        this.actionCodesList= this.actionCodesLookupData.ReferralCodes;
      break;
//some code

在代码中它没有显示错误,但测试未能显示错误为:

typeError:无法设置未定义的属性(设置“转介码”)

任何人都可以帮助我

while I am writing the unit test case for dropdown I am getting the error as
TypeError: Cannot set properties of undefined (setting 'ReferralCodes')
.spec.ts

it("should update the action selecting a value from category drop down", () => {
    component.actionCodesLookupData.ReferralCodes =[
      {
      Name: "some data",
      Id: 3,
      ActionStatus: 126
    }
    ]
    component.setActionCodesList("Referral");
    expect(component.actionCodesList).toBe(component.actionCodesLookupData.ReferralCodes);
       });

.component.ts

 public setActionCodesList(actionType: any):void{
   this.actionCodesList = [];
 
    switch(actionType){
      case ActionRecordConstant.memberContact:
      this.actionCodesList = this.actionCodesLookupData.MemberContactCodes;
     break;
      case ActionRecordConstant.referral:
        this.actionCodesList= this.actionCodesLookupData.ReferralCodes;
      break;
//some code

In code it is not showing the errors but test is failing showing the error as:

TypeError: Cannot set properties of undefined (setting 'ReferralCodes')

can anyone help me on this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我恋#小黄人 2025-02-07 18:22:31

我认为您的问题是ActionCodesLookupData在设置它时是不确定的。

尝试将其设置为空对象。

it("should update the action selecting a value from category drop down", () => {
    // !! add this line   
    component.actionCodesLookupData = {};
 
    component.actionCodesLookupData.ReferralCodes =[
      {
      Name: "some data",
      Id: 3,
      ActionStatus: 126
    }
    ]
    component.setActionCodesList("Referral");
    
 expect(component.actionCodesList).toBe(component.actionCodesLookupData.ReferralCodes);
});

I think your issue is that actionCodesLookupData is undefined when you set it.

Try setting it to an empty object first.

it("should update the action selecting a value from category drop down", () => {
    // !! add this line   
    component.actionCodesLookupData = {};
 
    component.actionCodesLookupData.ReferralCodes =[
      {
      Name: "some data",
      Id: 3,
      ActionStatus: 126
    }
    ]
    component.setActionCodesList("Referral");
    
 expect(component.actionCodesList).toBe(component.actionCodesLookupData.ReferralCodes);
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文