NullInjectorError: R3InjectorError(DynamicTestModule)[FormBuilder -> FormBuilder]:NullInjectorError: Noprovider for FormBuilder!


While doing Angular unit testing I got the following error:

b NullInjectorError: R3InjectorError(DynamicTestModule)[FormBuilder -> FormBuilder]:NullInjectorError: Noprovider for FormBuilder!

You can resolve the following error by importing the following modules,

import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';

Here is the complete spec file,

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { ServService } from './serv.service';


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

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.componentInstance;
    expect(app).toBeTruthy();
  });

});