javascript - Angular 4( karma )测试输入字段的输入值

标签 javascript angular unit-testing karma-jasmine

我正在尝试测试通过键盘输入值的事件。我的问题是在为输入字段设置值之后,当我打印它时打印它,但是当我在 whenStable() 中打印它时它打印为空。我想知道为什么这个值在 Whitstable() 函数中被重置。我该如何解决?

我提到了:Updating input html field from within an Angular 2 test编写这个测试用例。

it('Test input field value. ', async () => {
    const divDescription = fixture.debugElement.query(By.css('#costDescription'));

    divDescription.nativeElement.value = 'text';
    divDescription.nativeElement.dispatchEvent(new Event('input'));
    fixture.detectChanges();
    console.log('sendInput : ', divDescription.nativeElement.value); // prints 'text'
    fixture.whenStable().then(() => {
      console.log('sendInput : ', divDescription.nativeElement.value); // prints ''
      expect(divDescription.nativeElement.value).toContain('text');
    });
  });

最佳答案

删除 WhenStable() 会发生这种情况。

  it('Test input field value. ', async () => {
      const divDescription = fixture.debugElement.query(By.css('#costDescription'));
      divDescription.nativeElement.value = 'text';
      divDescription.nativeElement.dispatchEvent(new Event('input'));
      fixture.detectChanges();
      expect(divDescription.nativeElement.value).toContain('text');
  });

关于javascript - Angular 4( karma )测试输入字段的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52325439/

相关文章:

angular - 在 'https://login.microsoftonline.com/xxx from origin ' http ://localhost:4200' has been blocked by CORS policy 访问 XMLHttpRequest

c# - 如何使用“许多”脚本目录设置 Unity 单元测试

java - 单元测试类的最佳实践,主要负责调用依赖项的方法,但也包含逻辑

javascript - 为什么 mongoose.connect() 在不作为 mongoose 对象的方法存在时工作得很好?

javascript - ngFor 中的复选框,每次单击任何其他复选框时,只有第一个复选框被选中和取消选中

javascript - Hammer js 嵌套事件?

javascript - 从窗口中选择的文本为空

ruby-on-rails - 在不删除测试数据库的情况下运行 Rails 测试

javascript - Jquery.each抛出错误: possible Json to string conflict

javascript - 期望不正确的智能感知方法