Angular - 禁用输入的 Karma 测试无法正常工作

Chr*_*odz 2 typescript karma-runner karma-jasmine angular

我正在尝试测试一个开关组件,它有一个可以使用@Input() disabled属性禁用的输入。所以我编写了这个测试,它获取原始模型值,更新组件的disabled属性,模拟输入的单击,然后检查模型是否更改。

这不起作用:

it('should not update the model if disabled is true', () => {

  const model = !!component.model;

  component.disabled = true;

  fixture.detectChanges();

  input.click();

  expect(component.model).toBe(model);
});
Run Code Online (Sandbox Code Playgroud)

但是,如果我直接在输入上设置禁用状态input.disabled = true,则它可以工作。

奇怪的是,在 Karma 浏览器选项卡中,我可以看到该开关实际上已禁用。

我究竟做错了什么?

小智 5

将代码包裹在fixture.detectChanges();in之后fixture.whenStable().then(...)

it('should not update the model if disabled is true', () => {

  const model = !!component.model;

  component.disabled = true;

  fixture.detectChanges();

  fixture.whenStable().then(() => {
    input.click();
    expect(component.model).toBe(model);
  });
});
Run Code Online (Sandbox Code Playgroud)