Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在测试中未设置正确的表单验证类_Javascript_Angular_Typescript_Unit Testing_Jasmine - Fatal编程技术网

Javascript 在测试中未设置正确的表单验证类

Javascript 在测试中未设置正确的表单验证类,javascript,angular,typescript,unit-testing,jasmine,Javascript,Angular,Typescript,Unit Testing,Jasmine,在单元测试环境中应用验证类似乎存在问题。我所说的验证类是指ng有效和ng无效。我使用的角度版本是8.2.14 让我们以该组件为例: import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <form ngForm> <input type="text" required name="firstName" [(ngModel)]="fi

在单元测试环境中应用验证类似乎存在问题。我所说的验证类是指
ng有效
ng无效
。我使用的角度版本是
8.2.14

让我们以该组件为例:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
  <form ngForm>
    <input type="text" required name="firstName" [(ngModel)]="firstName"/>
    <button type="submit">Submit</button>
  </form>
  `
})
export class AppComponent {
  firstName = '';
}
记录的元素将具有类名:
ng未触及ng原始ng有效
。我希望输入在测试浏览器中无效,就像在没有测试配置的情况下呈现一样

我的问题是,为什么在验证类中存在这样的差异,以及如何在测试环境中实现正确的验证


提前谢谢

我认为您需要使
it
spec异步,因为它里面有一些异步代码

此外,您还需要使用
fixture.autoDetectChanges()
或在
稳定时运行
fixture.detectChanges()

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        RouterTestingModule,
        FormsModule
      ],
      declarations: [
        AppComponent
      ],
    }).compileComponents();
  }));

  it('should apply the correct validation classes', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();

    fixture.detectChanges();

    fixture.whenStable().then(() => {
      console.log(fixture.nativeElement.querySelector('input'));
    });
  });
});