首页 > 解决方案 > Angular Unit Testing面临初始化表单和订阅表单值变化的问题

问题描述

我是角度单元测试的新手,实际上我在初始化表单组时遇到了问题

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

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

在此函数中调用 initOrganizationForm 函数时有 catch

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

所以因为这段代码我收到了这个错误

 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)

标签: angularunit-testingangular-unit-test

解决方案


推荐阅读