我如何监视Typescript的获取器和设置器? [英] How do I spyOn Typescript getters and setters?

查看:65
本文介绍了我如何监视Typescript的获取器和设置器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我进行单元测试时,我的吸气剂是Typescript的二传手,我找不到监视这些吸气剂和二传手的方法.而是,该对象立即得到评估.我正在使用Jasmine进行单元测试.

When I unit test my getters are setters for Typescript, I cannot find a way to spy on those getters and setters. Instead, the object immediately gets evaluated. I am using Jasmine to unit test.

推荐答案

尚不支持,但是如果您现在真的需要支持,则可以扩展 SpyRegistry .js 文件并添加 apsillers 建议的代码:

If you really need the support now, you can extend SpyRegistry.js file and add the code that apsillers proposed:

this.spyOnProperty = function(obj, methodName, accessType) {
    ...
    var desc = Object.getPropertyDescriptor(obj, methodName);
    if(desc[accessType]) { // "get" or "set" exists on the property
        var spy = j$.createSpy(methodName, desc[accessType]);  

        desc[accessType] = spy;

        Object.defineProperty(obj, methodName, desc);
    }
}

这篇关于我如何监视Typescript的获取器和设置器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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