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

问题描述 投票:0回答:3

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

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

我用谷歌搜索了一下,没有找到任何东西。我做错了什么?任何帮助,将不胜感激。提前致谢!

编辑:将测试更新为:

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”

angular unit-testing karma-jasmine
3个回答
0
投票

尝试使用

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

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

     })

 }

0
投票

我遇到过同样的问题,解决我的方法是避免用未定义的(

array: undefined
)初始化数组,而是使用
array: []
.

© www.soinside.com 2019 - 2024. All rights reserved.