我如何使用茉莉花来监视getter属性? [英] How can I spy on a getter property using jasmine?

查看:137
本文介绍了我如何使用茉莉花来监视getter属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jasmine窥探getter属性?

How can I spy on a getter property using jasmine?

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

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

这也不起作用AFAICT:

This also does not work AFAICT:

spyOn(Object.getOwnPropertyDescriptor(o, 'foo'), 'get').and.returnValue('bar');


推荐答案

自Jasmine 2.6以来, href =https://jasmine.github.io/api/edge/global.html#spyOnProperty =noreferrer> spyOnProperty 。要监视 foo 属性的访问者,请执行:

Since Jasmine 2.6, this has been possible with spyOnProperty. To spy on the accessors for the foo property, do:

spyOnProperty(o, 'foo')

这允许你替换 set 和/或获取具有间谍功能的访问者属性的访问器函数。您可以指定或设置获取仅作为第三个参数:

This allows you to replace the set and/or get accessor functions for an accessor property with a spy function. You can specify or set or get only as a third argument:

spyOnProperty(o, 'foo', 'get')

如果您因使用早期版本而无法升级且由于某种原因无法升级,您可以合并将添加此功能的请求添加到您的本地代码副本中。

If you are stuck using an earlier version and cannot upgrade for some reason, you may be able to merge the pull request that added this feature into your local copy of the code.

这篇关于我如何使用茉莉花来监视getter属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆