亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在單元測試中注入 Input()?

如何在單元測試中注入 Input()?

哈士奇WWW 2021-12-23 10:43:36
所以我嘗試對組件進行一些單元測試。但是我對 Input() 參數有一些問題。然后特別是組件中的一個組件所以我有這個組件:export class EcheqDisplayComponent implements OnInit {  echeq: EcheqSubmissionApi;  constructor(    private route: ActivatedRoute  ) {    this.echeq = this.route.snapshot.data['submission'];  }  ngOnInit() {  }  getAnswers(page: EcheqPageApi): any[] {    return page.elements.map(element => this.echeq.answers[element.name]);  }}和模板:<div class="echeq-display" *ngIf="echeq">  <header class="echeq-display-header header">    <div class="echeq-display-info">      <h1 class="heading echeq-display-heading">        {{ echeq.definition.title }}      </h1>      <div class="sub-heading echeq-display-subheading">        <span class="echeq-display-creator">          Toegekend door:          {{ echeq.assignedByProfName ? echeq.assignedByProfName : 'Het Systeem' }}        </span>        <span class="echeq-display-date">{{          echeq.definition.createdOnUtc | date: 'dd MMM'        }}</span>      </div>    </div>    <app-meta-box      [metadata]="{        numPages: echeq.definition.numPages,        vPoints: echeq.definition.awardedVPoints      }"    ></app-meta-box>  </header>  <main class="echeq-display-questions body">    <app-echeq-question      *ngFor="let page of echeq.definition.pages; let i = index"      [page]="page"      [readonly]="true"      [number]="i + 1"      [answers]="getAnswers(page)"    ></app-echeq-question>  </main></div>但它一直在說:類型錯誤:無法讀取未定義的屬性“numPages”。那么我必須改變它的工作原理嗎?
查看完整描述

3 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

你有你的錯誤 numPages: echeq.definition.numPages。Endeed,echeq也是未定義的。


你可以試試 :


component.echeq = {

   definition: {

     numPages: 9

   }

}

或者更好的方法是從this.route.snapshot.data['submission'];so返回這個值MockActivatedRoute


更新:


并更新MockActivatedRoute以允許動態參數:


export class MockActivatedRoute {

  snapshot = {

      data: {}

  };


  constructor(){}


  withData(data:any): MockActivatedRoute {

     this.snapchot.data = data;

     return this;

 }

}

所以現在在你的測試中,你可以使用它:


{ provide: ActivatedRoute, useValue: new MockActivatedRoute().withData({submission:{ answers:{} } }) }



查看完整回答
反對 回復 2021-12-23
?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

沒問題。這是一個簡單的:


beforeEach(async(() => {

    TestBed.configureTestingModule({

      providers: [

      ],

      imports:[

        ParticipantEcheqModule,

        RouterTestingModule

      ]

    })

    .compileComponents();

  }));


查看完整回答
反對 回復 2021-12-23
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

回答你的問題:

component.metadata = /// whatever you want this to be


查看完整回答
反對 回復 2021-12-23
  • 3 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號