0

我有以下代码,

** 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

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

4

1 回答 1

0

您可以使用neverMock初始化FormGroup模块

import { FormsModule} from '@angular/forms';

 describe('FeedbackOptionsComponent', () => {

 Shallow.neverMock(FormsModule);

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

  });
 

于 2020-01-23T02:00:32.073 回答