Jasmine:无法设置isMultiple of undefined

时间:2018-02-14 12:34:34

标签: angular jasmine karma-jasmine

我的测试中出现错误:

Failed: Cannot set property 'isMultiple' of undefined

我的测试脚本很标准 - 它看起来像是这样:

  beforeEach(() => {
    fixture = TestBed.createComponent(DropdownTypeaheadControlComponent);
    component = fixture.componentInstance;
    component.multipleStringSeperator = '|';
    component.isMultiple = true;

    fixture.detectChanges();
  });

  // This test fails.
  it('should be created', () => {
    expect(component).toBeTruthy();
  });

在我的组件中有一个名为isMultiple set的属性。但是,我不明白为什么我的组件是undefined

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

好的,我修好了。对我来说,这是因为Input参数具有属性isMultiple,并且输入参数未定义。所以我所要做的就是在beforeEach()部分定义属性:

component.question = {
    isMultiple: false
}

感谢您的帮助:)

答案 1 :(得分:0)

你试过

吗?
fixture = TestBed.createComponent(DropdownTypeaheadControlComponent);
component = fixture.componentInstance;
fixture.detectChanges();

component.multipleStringSeperator = '|';
component.isMultiple = true;

???

答案 2 :(得分:0)

您应该移动it块内的组件的所有分配。

  beforeEach(() => {
    fixture = TestBed.createComponent(DropdownTypeaheadControlComponent);
    fixture.detectChanges();
  });

  // Test should not fail now :)
  it('should be created', () => {
    fixture.componentInstance.multipleStringSeperator = '|';
    fixture.componentInstance.isMultiple = true;
    fixture.detectChanges();
    expect(component).toBeTruthy();
  });