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

时间:2018-05-18 12:07:22

标签: angular jasmine

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

  

TypeError:无法在DomicileSelectionComponent.isMinValid读取未定义的属性'maxSize'(webpack:/// C:/vstsprojects/Risk.Analytics.Captives/Clientside/captives/src/app/pages/feasibility/ca   ptives /住所选择/住所-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 个答案:

答案 0 :(得分:1)

我猜问题就在那里:

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);