Search code examples
angularangular2-testing

Testing forms: angular2 testing


I'm trying to test a component that has imported 'FormGroup' and 'FormBuilder', but when I'm trying to run the test file for that component it gives me an error saying 'FormGroup' and 'FormBuilder' isn't a known property of form. I tried doing something like this,

TestBed.configureTestingModule({
imports: [ ],
declarations: [ FormComponent ],
providers: [ FormGroup, FormBuilder ]
})

but then it gives the following error:

Uncaught Failed: Can't resolve all parameters for FormGroup: (?, ?, ?).
Error: Can't resolve all parameters for FormGroup: (?, ?, ?).

So how can I import formgroup and formbuilder in the testing (spec) file?

Angular Version I'm using: 2.1.0


Solution

  • it gives me an error saying 'FormGroup' and 'FormBuilder' isn't a known property of form.

    The same way you imported ReactiveFormsModule into your application module(s), you need to do the same in the testing module

    TestBed.configureTestingModule({
      imports: [ ReactiveFormsModule ],
      declarations: [ FormComponent ]
    })