角度单元测试输入值

我一直在阅读单元测试(https://angular.io/docs/ts/latest/guide/testing.html)的官方Angular2文档,但我正在努力设置组件的输入字段值,以便将其反映在组件属性(通过ngModel绑定)中。屏幕在浏览器中运行良好,但在单元测试中,我似乎无法设置字段的值。

我正在使用下面的代码。"fixture“被正确初始化,因为其他测试运行良好。"comp“是我的组件的实例,输入字段通过ngModel绑定到"user.username”。

it('should update model...', async(() => {
    let field: HTMLInputElement = fixture.debugElement.query(By.css('#user')).nativeElement;
    field.value = 'someValue'
    field.dispatchEvent(new Event('input'));
    fixture.detectChanges();

    expect(field.textContent).toBe('someValue');
    expect(comp.user.username).toBe('someValue');
  }));

我的Angular2版本:"@angular/core": "2.0.0"

转载请注明出处:http://www.mingyanggongyi.com/article/20230526/1006829.html