Angular 7: ngModel数据绑定在单元测试中不能更新组件属性
创始人
2024-10-16 22:31:05
0

在单元测试中,使用fixture.detectChanges()来触发Angular变更检测,并更新组件属性。

下面是一个示例代码,展示如何在单元测试中更新组件属性:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [MyComponent]
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should update component property using ngModel', () => {
    const inputElement = fixture.nativeElement.querySelector('input');
    inputElement.value = 'New Value';
    inputElement.dispatchEvent(new Event('input'));
    fixture.detectChanges();

    expect(component.myProperty).toBe('New Value');
  });
});

在上面的示例中,我们使用fixture.detectChanges()来触发Angular变更检测,并更新组件属性。然后,我们获取输入元素,并模拟输入新的值。接着,我们再次调用fixture.detectChanges()来更新组件,并验证组件属性是否正确更新。

相关内容

热门资讯

透视详情!wpk官网下载链接(... 透视详情!wpk官网下载链接(透视)有没有人wepoker,教程法门(真的有挂)-哔哩哔哩1、wpk...
德扑圈有透视吗!wepoker... 德扑圈有透视吗!wepoker透视最简单三个步骤(透视)脚本-真是推荐真的有挂1、不需要AI权限,帮...
透视普及!德普之星辅助器怎么用... 透视普及!德普之星辅助器怎么用(透视)wepokerplus辅助,教程积累(有挂透视)-哔哩哔哩1、...
hhpoker有没有辅助!德扑... hhpoker有没有辅助!德扑HHpoker有挂吗(透视)软件-果然总结存在有挂1、进入游戏-大厅左...
hhpoker透视码怎么用!约... hhpoker透视码怎么用!约局吧开挂(透视)器-一贯揭幕有挂1、超多福利:超高返利,海量正版游戏,...
透视了解!wejoker开挂(... 透视了解!wejoker开挂(透视)wpk真的有透视嘛,教程窍门(有挂方式)-哔哩哔哩1、wpk真的...
wejoker黑侠辅助器!we... wejoker黑侠辅助器!wepoker-h5下载(透视)app-确实透视真的是有挂1、超多福利:超...
透视必备!hhpoker买挂(... 透视必备!hhpoker买挂(透视)aapoker免费透视脚本,教程秘籍(有挂工具)-哔哩哔哩1、a...
哈糖大菠萝有挂吗!hhpoke... 哈糖大菠萝有挂吗!hhpoker万能辅助器(透视)技巧-确实必备存在有挂1、不需要AI权限,帮助你快...
透视开挂!aapoker辅助器... 透视开挂!aapoker辅助器是真的吗(透视)聚星ai辅助工具下载,教程机巧(有挂细节)-哔哩哔哩运...