如何在 Angular 中使用 Karma-Jasmine 进行 videojs 录制功能的单元测试?

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

我正在尝试在 Video.js 中对开始记录进行单元测试。 我为 video.js 创建了一个模拟播放器对象。 当我对它进行单元测试时,它崩溃了。

代码

      authBtn() { 
        if(this.deviceId ==''){
          this.helper.toastErrorMessage('Please Select Camera');
          return;
        }
        playerStatus = true;
        player.record().start();
      }
// Unit Test Code
      describe('AuthenticateCandidateComponent', () => {
      it('should display error message and return if deviceId is empty', () => {
        spyOn(component.helper, 'toastErrorMessage');
        component.deviceId = ''; 
        component.playerStatus=true;
        component.authBtn(); expect(component.helper.toastErrorMessage).toHaveBeenCalledWith('Please Select Camera');
        expect(component.playerStatus).toBeTrue();
      });
    
      it('should start recording when authBtn is called', () => {
        component.player = {
          record: jasmine.createSpyObj('record', ['start']), 
        };
        component.authBtn();
        expect(component.playerStatus).toBe(true);
        expect(component.player.record().start).toHaveBeenCalled(); // Unit Test breaks here 
      });
      
      });

错误和堆栈跟踪

TypeError: Cannot read properties of undefined (reading 'record')
    at AuthenticateCandidateComponent.selectedData (http://localhost:9876/_karma_webpack_/webpack:/src/app/views/proctoring/authenticate-candidate/authenticate-candidate.component.ts:221:5)
    at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/views/proctoring/authenticate-candidate/authenticate-candidate.component.spec.ts:369:15
angular typescript unit-testing karma-jasmine video.js
© www.soinside.com 2019 - 2024. All rights reserved.