How to spyOn a value property (rather than a method) with Jasmine

前端 未结 10 2253
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 04:12

Jasmine\'s spyOn is good to change a method\'s behavior, but is there any way to change a value property (rather than a method) for an object? the code could be

10条回答
  •  情书的邮戳
    2020-12-01 04:46

    Jasmine doesn't have that functionality, but you might be able to hack something together using Object.defineProperty.

    You could refactor your code to use a getter function, then spy on the getter.

    spyOn(myObj, 'getValueA').andReturn(1);
    expect(myObj.getValueA()).toBe(1);
    

提交回复
热议问题