【问题标题】:Cannot read property of undefined jasmine test无法读取未定义茉莉花测试的属性
【发布时间】:2018-05-18 20:16:15
【问题描述】:

我在 jasmine 中为我的 Angular 应用程序编写了一个测试,该应用程序失败了,但实际功能绝对正常。我也在调用 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 出现错误?

测试组件

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

【问题讨论】:

  • comp.isMinValid(comp.domicileInfo.taxAssesment.items, 0);上给出整个数组正常吗?
  • 数组中应该只有一项
  • 我得到了答案。我刚刚传递了一个数组项,它工作了

标签: angular jasmine


【解决方案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);

【讨论】:

    猜你喜欢
    • 2018-12-08
    • 1970-01-01
    • 2021-09-28
    • 2019-04-06
    • 2013-11-14
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多