【问题标题】:this.fb.group({ is not a function in angular?this.fb.group({ 不是角度函数?
【发布时间】:2020-05-09 05:24:32
【问题描述】:

我有以下代码,

** component.ts,

 constructor(public cdr:ChangeDetectorRef, public fb:FormBuilder) { 
   this.buildForm();
 }



buildForm(): void {
 this.feedbackForm = this.fb.group({
  suggestedAScore: ['', [Validators.pattern(/^[0-9]+$/),Validators.required, Validators.maxLength(5), Validators.minLength(5)]],
  minScore: ['', [Validators.pattern(/^[0-9]+$/),Validators.required,Validators.minLength(1),Validators.maxLength(1)]],
  maxScore: ['', [Validators.pattern(/^[0-9]+$/),Validators.required,Validators.minLength(1),Validators.maxLength(1)]]
});
 this.feedbackForm.setValidators(this.minMaxValidator());
this.isFeedbackFormValid();
};

** spec.ts,(浅渲染)

 describe('FeedbackOptionsComponent', () => {
  let shallow: Shallow<FeedbackOptionsComponent>;

 beforeEach(() => {
   shallow = new Shallow(FeedbackOptionsComponent, AnnualCompensationModule)
   .provide(FormBuilder)
 });

  it('should create', () => {
    expect(FeedbackOptionsComponent).toBeTruthy();
 });

当我运行代码时,我得到了测试用例,我得到了以下错误:

this.fb.group({ is not a function

我是单元测试用例的新手。任何人都可以建议我帮助解决这个问题。

【问题讨论】:

    标签: angular karma-jasmine angular2-forms mean angular-unit-test


    【解决方案1】:

    你可以使用neverMock来初始化FormGroup模块

    import { FormsModule} from '@angular/forms';
    
     describe('FeedbackOptionsComponent', () => {
    
     Shallow.neverMock(FormsModule);
    
       beforeEach(() => {
       shallow = new Shallow(FeedbackOptionsComponent, AnnualCompensationModule);
     });
    
      });
     

    【讨论】:

      猜你喜欢
      • 2021-05-02
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 2018-04-19
      • 2016-11-07
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      相关资源
      最近更新 更多