使用 jasmine.createSpyObj 实用程序构建 SpyObj 时声明 getter 属性?

2024-02-09

假设我有一堂课:

class MyRealClass {
  get propOne() { return stuffFromTheServer; }
}

测试的时候我想实现这个功能:

const mockClass = {
  get propOne() { return someStuff; }
}

jasmine.spyOnProperty(mockClass, 'propOne', 'get');

通过做这样的事情...

const spy = jasmine.createSpyObj('mockClass', [
  {methodName: 'propOne', accessType: 'get'}
]);

换句话说,我想建立一个SpyObj<MyRealClass>使用jasmine.createSpyObj并将 getter 属性声明为方法methodName数组(第二个参数createSpyObj() method.

这可能吗?


createSpyObj采用可选的最后一个参数,可让您声明属性:

const spy = jasmine.createSpyObj(['here', 'be', 'methods'], { propOne: 'someStuff' });

or

const spy = jasmine.createSpyObj('mockClass', ['here', 'be', 'methods'], { propOne: 'someStuff' });

See here https://jasmine.github.io/api/edge/jasmine.html#.createSpyObj and here https://jasmine.github.io/tutorials/spying_on_properties对于官方文档

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jasmine.createSpyObj 实用程序构建 SpyObj 时声明 getter 属性? 的相关文章

随机推荐