首页 > 解决方案 > 如何修复玩笑测试 TypeError:无法设置只有 getter 的 [object Object] 的属性 selectStoreEntity$?

问题描述

如何开玩笑地测试这个只读的可观察变量?

我尝试按照本文的指示进行操作-> https://medium.com/angular-in-depth/how-to-test-observables-a00038c7faad

运行时出现此错误: Cannot set property selectStoreEntity$ of [object Object] which has only a getter

语音-uc.service.spec.ts

 // whether or not the account has uc-voice
  readonly hasVoiceUc$ = this.inventory$.pipe(
    map(inventory => inventory?.data?.data?.length > 0)
  );

语音-uc.service.spec.ts

it('should call hasVoiceUc$', async () => {
    // Testing using marbles (see https://medium.com/angular-in-depth/how-to-test-observables-a00038c7faad)
    const scheduler = new TestScheduler((actual, expected) => {
      expect(actual).toEqual(expected);
    });

    scheduler.run(({ cold, expectObservable }) => {
      const values = {
        a: null,
        b: {
          data: {},
        },
        c: {
          data: {
            data: [],
          },
        },
        d: {
          data: {
            data: ['value'],
          },
        },
      };
      const source$ = cold('abcd|', values);
      const expectedMarble = 'abcd|';
      const expectedValues = { a: false, b: false, c: false, d: true };

      (selectStoreEntity$ as any) = () => source$;

      service = new VoiceUcService({} as any, loggerService);

      const result$ = service.hasVoiceUc$;
      expectObservable(result$).toBe(expectedMarble, expectedValues);
    });
  });

错误:TypeError:无法设置只有 getter 的 [object Object] 的属性 selectStoreEntity$

● VoiceUcService › should call hasVoiceUc$

    TypeError: Cannot set property selectStoreEntity$ of [object Object] which has only a getter

       99 |       const expectedValues = { a: false, b: false, c: false, d: true };
      100 | 
    > 101 |       (selectStoreEntity$ as any) = () => source$;
          |                                  ^
      102 | 
      103 |       service = new VoiceUcService({} as any, loggerService, {} as any);
      104 | 

标签: angularunit-testingjestjs

解决方案


推荐阅读