How to get Jasmine's spyOnProperty to work?

前端 未结 2 1189
Happy的楠姐
Happy的楠姐 2020-12-17 10:17

I saw this post post and was excited to try it out, but I\'m unable to get it working. Trying to keep this simple just to figure out what\'s wrong, but even this is failing.

2条回答
  •  北海茫月
    2020-12-17 10:58

    I was still struggling a bit to get the set to work.

    const foo = {
      get value() {},
      set value(v) {}
    };
    
    it('can spy on getters', () => {
      spyOnProperty(foo, 'value', 'get').and.returnValue(1);
      expect(foo.value).toBe(1);
    });
    
    it('and on setters', () => {
      const spiez = spyOnProperty(foo, 'value', 'set');
      foo.value = true;
      expect(spiez).toHaveBeenCalled();
    });
    

提交回复
热议问题