dimanche 3 mars 2019

Nullinjectorerror: no provider for formbuilder (I'm importing ReactiveFormsModule)

So I'm trying to write a really basic test for a component. I just want to make sure that the form that I'm creating using FormBuilder is an instance of FormGroup, but I'm consistently getting a NullInjectorerror: No provider for FormBuilder, and I've tried pretty much everything. I made sure I was importing the ReactiveFormsModule.

hours-calculator.component.ts

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-hours-calculator',
  templateUrl: './hours-calculator.component.html',
  styleUrls: ['./hours-calculator.component.scss']
})
export class HoursCalculatorComponent implements OnInit {

  constructor(private formBuilder: FormBuilder) { }

  form: FormGroup;
  
  ngOnInit() {
      this.form = this.formBuilder.group({
         control1: this.formBuilder.control('')
      });
  }
}

hours-calculator.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormGroup, ReactiveFormsModule, FormsModule, FormBuilder } from '@angular/forms';
import { HoursCalculatorComponent } from './hours-calculator.component';

describe('HoursCalculatorComponent', () => {
  let component: HoursCalculatorComponent;
  let fixture: ComponentFixture<HoursCalculatorComponent>;

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

  beforeEach(() => {
    fixture =   TestBed.createComponent(HoursCalculatorComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create a form using formbuilder', () => {
    expect(component.form instanceof FormGroup).toBeTruthy();
  });
}

Am I just missing something extremely obvious? I'm really tearing my hair out over this one. I'm really new to unit testing angular components, so explain it to me like I'm 5 :) For the record, I'm using angular 7.

Aucun commentaire:

Enregistrer un commentaire