How can I spy on a getter property using jasmine?

前端 未结 6 1857
南笙
南笙 2020-12-14 06:02

How can I spy on a getter property using jasmine?

var o = { get foo() {}, };

spyOn(o, \'foo\').and.returnValue(\'bar\'); // Doesn\'t work.

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 06:16

    You can do this if you are unable to use the latest jasmine (2.6.1)

    const getSpy = jasmine.createSpy().and.returnValue('bar')
    Object.defineProperty(o, 'foo', { get: getSpy });
    

    Documentation for defineProperty, here

提交回复
热议问题