无法读取未定义的茉莉花测试的属性

我已经在茉莉花中为我的角度应用程序编写了一个测试,但这个测试失败但实际的功能非常好 . 我也在调用fixture.DetectChanges()但不确定问题是什么 . 我收到了一个错误

TypeError:无法在DomicileSelectionComponent.isMinValid读取未定义的属性'maxSize'(webpack:/// C:/vstsprojects/Risk.Analytics.Captives/Clientside/captives/src/app/pages/feasibility/ca ptives / domicile-selection /domicile-selection.component.ts?:88:52)

为什么错误只出现在maxSize而不是minSize?

TestComponent

describe('DomicileSelectionComponent', () => {
  let comp: DomicileSelectionComponent;
  let fixture: ComponentFixture<DomicileSelectionComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        TooltipModule.forRoot(),
        FormsModule,
        TranslateModule.forRoot({
          loader: { provide: TranslateLoader, useClass: TranslateFakeLoader }
        })
      ],
      providers: [
        { provide: BsModalRef, useClass: BsModalRefStub },
        { provide: BackendProxy.ReferenceProxy, useClass: ReferenceProxyStub },
        { provide: RunService, useValue: runServiceStub }
      ],
      declarations: [DomicileSelectionComponent, YesNoPipe, CLICK_INPUT_DIRECTIVE, ShortNumberFormatPipe]
    });
  });

  beforeEach(() => {
   spyOn(BsModalRefStub.prototype, 'hide').and.callThrough();

    fixture = TestBed.createComponent(DomicileSelectionComponent);
    comp = fixture.componentInstance;
    comp.domicileInfo = domicileInformationDataResult.data;
    fixture.detectChanges();
  });

  fit('should return true because the index of the item is zero and min is less than max', () => {
    comp.domicileInfo.taxAssesment.items = [{  minSize: 30000000, maxSize: 40000000, values: [0.02, 0.02]}];
    fixture.detectChanges();
    console.log(comp.domicileInfo.taxAssesment.items);
    let isMin: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items, 0);
    expect(isMin).toBe(true);
  });

});

主要成分

isMinValid(currentItem: any, index: number) {
    if (index === 0 && (+currentItem.minSize <= +currentItem.maxSize)) {
        return !this._isMinValid;
    }
    else if ( index === 0 && +currentItem.minSize >= +currentItem.maxSize) {
          return this._isMinValid;
    } 
    let previousItem = this.domicileInfo.taxAssesment.items[index - 1];
    if (+currentItem.minSize !== +previousItem.maxSize) {
     return this._isMinValid;
    }
    return !this._isMinValid;
  }

回答(1)

3 years ago

我猜问题就在那里:

fit('should return true because the index of the item is zero and min is less than max', () => {
    comp.domicileInfo.taxAssesment.items = [{  minSize: 30000000, maxSize: 40000000, values: [0.02, 0.02]}];
    fixture.detectChanges();
    console.log(comp.domicileInfo.taxAssesment.items);
    let isMin: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items, 0);
    expect(isMin).toBe(true);
});

你给isMinValid整个数组( comp.domicileInfo.taxAssesment.items . )

试试这个 :

let isMin: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items[0], 0);