无法读取未定义的属性“ findIndex”

时间:2018-10-22 10:30:48

标签: angular unit-testing karma-jasmine

我正在尝试测试应用程序中的一个组件。在该组件的构造函数中,我有一个函数findIndex,类似这样:

  dels: Del[];
  selectedDel: number = null; // It is a index
  selectedMun: number = null; // It is a index

    constructor(private _emitService: EmitService, private _locService: LocService) {
        let data = <Perfil>JSON.parse(localStorage.getItem('data'));
        this.dels = data.dels;
        this.selectedDel = this.dels.findIndex(x => x.id_del == JSON.parse(localStorage.getItem('Del')));
        this.changeDel(this.selectedDel);
        this.selectedMun = JSON.parse(localStorage.getItem('Mun'));
        _emitService.filter({ idEvent: 'CloseSidenav' });
      }

我正在尝试做一个简单的“应该创建”,但是我收到此错误:

  

TypeError:无法读取未定义的属性'findIndex'

我的测试:

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

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

我也尝试过,但是没有成功:

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

  it('should create', () => {
    localStorage.setItem('data', '{"id_user":"1234edr","dels":[{"id_del":50,"desc":"DESC 1","mun":[]}');
    localStorage.setItem('Del', '11');
    fixture.detectChanges();
    expect(component).toBeTruthy();
  });
});

我用Google搜索它,但没有找到任何东西。我做错了什么?任何帮助,将不胜感激。预先感谢!

编辑:将测试更新为此:

describe('ChangeDelMunComponent', () => {
  let component: ChangeDelMunComponent;
  let fixture: ComponentFixture<ChangeDelMunComponent>;
    beforeEach(() => {
        TestBed.configureTestingModule({
          imports: [
            BrowserAnimationsModule,
            FormsModule,
            MatIconModule,
            MatCardModule,
            MatSelectModule,
            MatProgressBarModule,
            MatButtonModule,
            ToastrModule.forRoot(),
            HttpClientModule,
          ],
          declarations: [ChangeDelMunComponent],
          providers: [
            EmitService,
            LocalizacionesService
          ]
        })
          .compileComponents().then(() => {
            fixture = TestBed.createComponent(ChangeDelMunComponent);
            component = fixture.componentInstance;
            localStorage.setItem('data', '{"id_user":"1234edr","dels":[{"id_del":50,"desc":"DESC 1","mun":[]}');
            localStorage.setItem('Del', '11');
          });
      });

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

错误findIndex消失了,但是又出现了另一个错误:

  

TypeError:无法读取未定义的属性'detectChanges'

3 个答案:

答案 0 :(得分:0)

尝试使用

Object.values(dels).findIndex(what_you're_looking for)

答案 1 :(得分:0)

问题

您正在尝试从异步服务中获取数据,并在数据可用之前对其进行检索。

修复

Subscribe是从服务类返回的Observable

  

您的代码应该喜欢

 constructor(private _emitService: EmitService, private _locService: LocService) {
         = _locService.getData().subscribe(dels=>{ //subscribe the observable 
          this.dels = dels;
            this.selectedDel = this.dels.findIndex(x => x.id_del == JSON.parse(localStorage.getItem('Del')));
        this.changeDel(this.selectedDel);
        this.selectedMun = JSON.parse(localStorage.getItem('Mun'));
        _emitService.filter({ idEvent: 'CloseSidenav' });

     })

 }

答案 2 :(得分:0)

我遇到了同样的问题,解决我的是避免使用 undefined (<p>) 初始化数组,而是使用 array: undefined