0

i'm new in unit testing in angular actually i facing a issue while initialization of form group

  it('should be able to init forms', () => {

        service.organizationForm = formBuilder.group({})
        service.initOrganizationForm()
})

while initOrganizationForm function is called in this function there is catch

this.organizationForm.valueChanges.
            subscribe(form => {
                this.organizationButtonsData[1]['disabled'] = this.organizationForm.valid ? false : true;
                this.getRequiredCount();
            });

so becuase of this code i am getting this error

 should be able to init forms
     Setting Management Service
     TypeError: Cannot read property 'valueChanges' of undefined
    at <Jasmine>
    at SettingManagementService.valueChanges [as initOrganizationForm] (http://localhost:9876/_karma_webpack_/src/app/core/services/settings.management.service.ts:465:31)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/src/app/core/services/settings.management.service.spec.ts:74:17)
  • Normally your formgroup instance should usualy be instantiated when ngOnInit is called, so you should not have an issue like that if SettingManagementService.valueChanges comes after form intialisation in ngOnInit, to me you should create a test that the component is defined rather that testing a for form is inialized, that will come on itself. – HDJEMAI Jan 05 '21 at 10:59
  • Could you please provide a bit more information on where your form is created and how your test setup looks like. – Erbsenkoenig Jan 05 '21 at 11:30
  • @HDJEMAI actually i am writing test cases for a service – manish panchal Jan 05 '21 at 11:49
  • you have to add more code to your question, probably you are not calling `fixture.detectChanges();` if you do so the `the ngOnInit()` will be called and your form instance will be initialized .... – HDJEMAI Jan 05 '21 at 14:45

0 Answers0