angular - 测试期间 ng-reflect 与 nativeElement 上的 'real' 属性值

标签 angular testing karma-jasmine

我尝试使用 karma/jasmine nut 测试 angular4/5 中的自定义输入元素,我不知道是否遗漏了什么。

谁能给我指出正确的方向?

所以我有一个自定义输入组件,例如:

<div>
  <input class="input-class" [type]="inputType" [required]="required" [disabled]="disabled"          [(ngModel)]="value" (input)="handleChange()" />
  <label>...</label>
</div>

现在我想测试 nativeElement 上的 disabled 属性,例如:

it('can be disabled', () => {
formControl.disable();
    let inputElement = fixture.debugElement.query(By.css('input-element'))
    fixture.detectChanges();
    // works:
    expect(inputElement.attributes.getNamedItem('ng-reflect-is-disabled')).toBeTruthy();
    // works not:
    expect(inputElement.disabled).toBeTruthy();   
    // also doesn't work:
    expect(inputElement.attributes.getNamedItem('disabled')).toBeTruthy();
  });

为什么我可以在 ng-reflect 属性上看到更改,但在原始属性上看不到?我会错过触发生命周期事件的电话吗?我必须使用 Protractor 测试吗?

最佳答案

所以我想通了。

fixture.whenStable().then(() => {
  expect(inputElement().attributes.getNamedItem('disabled')).toBeTruthy();
  expect(inputElement().disabled).toBeTruthy();
});

变戏法。

关于angular - 测试期间 ng-reflect 与 nativeElement 上的 'real' 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48259215/

相关文章:

angular - Angular 客户端是否应该将用户角色存储在本地存储中?

flash - QTP for Flash - 有什么有用的插件推荐吗?

angular - 具有@input属性的单元测试angular 4指令

javascript - Jasmine 不注入(inject)服务

javascript - 什么被认为是测试返回 http observables 的方法的正确方法?

javascript - 表格中的响应式 c3 图表

angular - Tinymce 与 Angular 2/4 的双向绑定(bind)

testing - 在所有测试用例中使用相同的代码

javascript - 如何在 ngFor 中仅显示单个值或单个替代值?

testing - 如何处理几乎在软件验证结束时引入的错误修复?