【问题标题】:Input not rendered for ion-input in tests在测试中未为离子输入呈现输入
【发布时间】:2023-03-18 13:27:01
【问题描述】:

我有一个包含两个字段的简单表单:用户名和电子邮件。我的测试如下所示:

...
fit('should create', () => {
    console.log(fixture.debugElement.nativeElement.childNodes);
    expect(component).toBeTruthy();
  });
....

我刚刚将console.log 添加到生成的规范文件中。从下面的输出中可以看出,离子输入元素不包含输入元素。你知道为什么吗? (我需要它来运行附加到输入的模糊处理程序)

LOG: NodeList{0: <form _ngcontent-a-c0="" novalidate="" ng-reflect-form="[object Object]" class="ng-untouched ng-pristine ng-invalid"><div _ngcontent-a-c0=""><!--bindings={
  "ng-reflect-ng-if": "false"
}--><ion-item _ngcontent-a-c0="" class="ion-untouched ion-pristine ion-invalid"><!--bindings={
  "ng-reflect-ng-if": "true"
}--><ion-label _ngcontent-a-c0="" color="medium" position="floating" ng-reflect-color="medium" ng-reflect-position="floating">Username</ion-label><!--bindings={
  "ng-reflect-ng-if": "false"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--><!--bindings={
  "ng-reflect-ng-if": "false"
}--><ion-input _ngcontent-a-c0="" formcontrolname="username" type="text" ng-reflect-type="text" ng-reflect-name="username" class="ng-untouched ng-pristine ng-invalid ion-untouched ion-pristine ion-invalid"></ion-input></ion-item><ion-item _ngcontent-a-c0="" class="ion-untouched ion-pristine ion-valid"><!--bindings={
  "ng-reflect-ng-if": "true"
}--><ion-label _ngcontent-a-c0="" color="medium" position="floating" ng-reflect-color="medium" ng-reflect-position="floating">Email</ion-label><!--bindings={
  "ng-reflect-ng-if": "false"
}--><ion-input _ngcontent-a-c0="" formcontrolname="email" readonly="true" type="email" ng-reflect-readonly="true" ng-reflect-type="email" ng-reflect-name="email" class="ng-untouched ng-pristine ng-valid ion-untouched ion-pristine ion-valid"></ion-input></ion-item><ion-input _ngcontent-a-c0="" formcontrolname="token" type="hidden" ng-reflect-type="hidden" ng-reflect-name="token" class="ng-untouched ng-pristine ng-invalid ion-untouched ion-pristine ion-invalid"></ion-input></div><div _ngcontent-a-c0="" class="ion-padding-top"><ion-button _ngcontent-a-c0="" expand="block" size="large" type="submit" ng-reflect-disabled="true" ng-reflect-expand="block" ng-reflect-size="large" ng-reflect-type="submit">Register</ion-button></div></form>}

【问题讨论】:

  • 运行这个会发生什么:const inputDe = fixture.debugElement.query(By.css('ion-input[formControlName="something"]')); const nativeInput = await inputDe.componentInstance.getInputElement(); console.log(nativeInput);

标签: angular ionic-framework ionic4


【解决方案1】:

你可以使用:

await element(by.css('ion-input[formControlName="something"] input'))

获取可以与之交互的离子输入的输入。

【讨论】:

  • 如果 input 根本没有被渲染,我认为 css 选择器不会起作用
  • @jdaz 这是不可能的。它将被渲染。我确实测试过。
猜你喜欢
  • 2017-09-19
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
相关资源
最近更新 更多