【问题标题】:Sinon - spy on instanceofSinon - 监视 instanceof
【发布时间】:2018-08-19 18:56:48
【问题描述】:

如何监视 instanceof?我试着做:

let spy = sinon.spy(global, 'instanceof')

但这不起作用,我正在尝试对其进行测试以确保它被调用;

【问题讨论】:

  • instanceof 是运算符,而不是方法。但是,它没有多大意义,您应该检查使用它的结果,而不是它被调用的结果。

标签: node.js sinon instanceof spy


【解决方案1】:

我的意思是,如果你真的想要这样做,你可以在instanceof周围写一个简单的包装器:

utils.isInstanceOf = function(obj, cls) {
    return obj instanceof cls;
};


let spy = sinon.spy(utils, 'isInstanceOf')

然后你可以在你的代码中调用utils.isInstanceOf,而不是直接使用instanceof

【讨论】:

    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 2015-05-13
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 2013-07-21
    相关资源
    最近更新 更多