Angular 6单元测试:在响应式表单中更改pristine
创始人
2024-10-16 07:00:38
0

在Angular 6中,可以使用FormControlmarkAsDirty()方法来更改响应式表单中的pristine状态。下面是一个示例代码:

import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule, FormControl } from '@angular/forms';

describe('YourComponent', () => {
  let component: YourComponent;
  let fixture: ComponentFixture;
  let formControl: FormControl;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [ReactiveFormsModule],
      declarations: [YourComponent]
    }).compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(YourComponent);
    component = fixture.componentInstance;
    formControl = new FormControl();
    component.yourFormControl = formControl;
    fixture.detectChanges();
  });

  it('should mark form control as dirty', () => {
    expect(formControl.pristine).toBe(true);
    component.changeFormControl();
    expect(formControl.pristine).toBe(false);
  });
});

在上面的示例中,我们使用ReactiveFormsModule导入响应式表单模块,并创建了一个FormControl实例。然后,我们将这个FormControl实例分配给组件的一个变量yourFormControl。在测试用例中,我们首先断言formControl.pristinetrue,然后调用组件的changeFormControl()方法,该方法在内部会调用yourFormControl.markAsDirty()。最后,我们再次断言formControl.pristinefalse来验证pristine状态已经更改。

请注意,YourComponentchangeFormControl()是示例代码中的占位符,你需要将它们替换为你的实际组件和方法。

相关内容

热门资讯

智星德州插件最新版本更新内容详... 智星德州插件最新版本更新内容详解!wepokerplus透视(透视)方法-一直详情真的是有挂1、每一...
透视普及!德扑圈透视挂(透视)... 透视普及!德扑圈透视挂(透视)智星菠萝可以辅助吗,教程诀窍(果真有挂)-哔哩哔哩1、智星菠萝可以辅助...
wpk透视是真的吗!hhpok... wpk透视是真的吗!hhpoker可以开挂吗(透视)方法-本来解迷真的有挂wpk透视是真的吗!hhp...
透视曝光!we poker免费... 透视曝光!we poker免费辅助器(透视)德普之星透视辅助软件下载,教程指引(有挂细节)-哔哩哔哩...
wepoker安装教程!拱趴大... wepoker安装教程!拱趴大菠萝十三水透视(透视)器-好像有挂真的是有挂所有人都在同一条线上,像星...
透视教你!wepoker辅助插... 透视教你!wepoker辅助插件功能(透视)wpk有辅助器吗,教程诀窍(确实有挂)-哔哩哔哩1、we...
红龙poker辅助器免费观看!... 红龙poker辅助器免费观看!wepoker科技辅助器(透视)插件-果然解密真的有挂1)红龙poke...
透视解谜!wepoker有没有... 透视解谜!wepoker有没有插件(透视)poker master辅助,教程绝活(了解有挂)-哔哩哔...
wepoker俱乐部辅助!hh... wepoker俱乐部辅助!hhpoker可以开挂吗(透视)脚本-一直解密是有挂1、实时hhpoker...
透视分享!wpk辅助购买(透视... 透视分享!wpk辅助购买(透视)wepoker免费透视脚本,教程步骤(有挂助手)-哔哩哔哩1、wep...