Ошибка: - Не удается прочитать переменную свойства undefined во время модульного тестирования - PullRequest
0 голосов
/ 18 июня 2019

переменная из сервиса приходит как неопределенная

Ниже мой файл TS

ngOnInit() {

        this.serviceElectionForm.form = this.formBuilder.group({});

     /****
         * commenting below, if condition,  doesnt cause any error in spec file
         */
        if (this.registerService.submitReg.isAdminFlow) {
          this.companyDesignationForm.form = this.formBuilder.group({});
        }


        this.userAuthenticationForm.form = this.formBuilder.group({});
        this.roleDesignationForm.form = this.formBuilder.group({});

      }

ниже мой файл спецификаций

fdescribe('RegisterComponent', () => {
  let component: RegisterComponent;
  let fixture: ComponentFixture<RegisterComponent>;



  const MockRegisterService = {
    isAdminFlow: true,
  }

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        RegisterComponent,

      ],
      imports: [
        AngularMaterialsGlobalModule,
        RouterTestingModule,
        ReactiveFormsModule,
        NoopAnimationsModule,
        HttpClientTestingModule
      ],
      providers: [
        { provide: RegisterService, useValue: MockRegisterService },
        CookieService
      ]
    }).compileComponents();
  }));

  describe('Base instantiation and component creation', () => {

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

    it('should create register component ', () => {
      expect(component).toBeTruthy();
    });
  });

Я получаю ошибку

typeError: Невозможно прочитать свойство isAdminFlow из неопределенного TypeError: Невозможно прочитать свойство isAdminFlow из неопределенного в RegisterComponent ../ src / app / pages / registration / register.component.ts.RegisterComponent. ngOnInit

1 Ответ

0 голосов
/ 18 июня 2019

Попробуйте, ваша служба ожидает, что свойство с именем submitReg внутри этого свойства будет isAdminFlow

const MockRegisterService = {
    submitReg:{
     isAdminFlow: true,
    }
  }
...